>>> myfile=open('123.txt','w')
>>> myfile.write
<built-in method write of _io.TextIOWrapper object at 0x012D8D30>
>>> myfile.write('hello world \n')
13
>>> myfile.write('hello world 2 \n')
15
>>> myfile.close ()
>>> myfile=open('123.txt')
>>> myfile.readline
<built-in method readline of _io.TextIOWrapper object at 0x0170B5B0>
>>> myfile.readline ()
'hello world \n'
>>> myfile.readline ()
'hello world 2 \n'
>>> myfile.readline ()
''
>>>
上面是读取、写入文件
3.只有字符串才能够写入文件
>>> myfile=open('123.txt','w')
>>> l=(1,2,3)
>>> myfile.write(l)
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
myfile.write(l)
TypeError: must be str, not tuple
>>>
>>> t=[1,2,3]
>>> myfile.write(t)
Traceback (most recent call last):
File "<pyshell#35>", line 1, in <module>
myfile.write(t)
TypeError: must be str, not list
>>>