Python --文件的读写

1读文件

python 的读文件十分简单

f = open('/Users/michael/test.txt','r') r代表读文件 路径值要写绝对路径,写相对路径无法读入

然后

f.read()

f.close()

简单的文件读取的方法

with open('/Users/michael/test.txt','r') as f:

  printf(f.read())

 

按行读取的方法

for line in f.readflines():

  print(line.stricp()) #把末尾的'\n'删掉

 

2 写文件

with open('/Users/michael/test.txt','w') as f:

  f.write('Hello world!')

 





posted @ 2017-11-23 15:57  戴怪兽  阅读(152)  评论(0编辑  收藏  举报