文本文件:
hello
world!
this
is
a
test
text
read():从当前位置开始,读完所有的文件,返回字符串。
>>> myfile = open("test.txt","r")
>>> myfile.read()
'hello\nworld!\nthis \nis \na \ntest\ntext\n'
>>> myfile.read()
''
>>> myfile.close()
readline():只读下一行内容,返回字符串。
>>> myfile = open("test.txt","r")
>>> myfile.readline()
'hello\n'
>>> myfile.readline()
'world!\n'
>>> myfile.readline()
'this \n'
>>> myfile.readlines()
['is \n', 'a \n', 'test\n', 'text\n']
>>> myfile.close()
readlines():从当前位置,每次读取多行,返回列表。>>> myfile = open("test.txt","r")
>>> myfile.readlines()
['hello\n', 'world!\n', 'this \n', 'is \n', 'a \n', 'test\n', 'text\n']
>>> myfile.readline()
''
>>> myfile.close()