### 读取文件并加载成一行string
>>> with open('test.txt','rt') as f:
... data = f.read()
...
>>>
>>> data
'111\n222\n333\n\n'
### 读取文件中的每一行
>>> with open('test.txt','rt') as f:
... for line in f:
... print(line)
...
111
222
333
类似可以使用wt参数对文件进行写入操作,类似shell的 echo > 会抹去源数据
>>> with open('test.txt','wt') as f:
... f.write("ttt")
... f.write("yyy")
>>> with open('test.txt','rt') as f:
... f.read()
'tttyyy'
>>> with open('test.txt','wt') as f:
... print('wt',file=f)
>>> with open('test.txt','rt') as f:
... f.read()
'wt\n'
>>>
如果要在文件末尾追加数据,则可以使用at参数
如果要给文件使用其它的编码模式,则可以增加encoding参数
>>> with open('test.txt','rt',encoding='latin-1') as f:
...