Python 文件读写

 1 #读文件:
 2 #第一种方法:
 3 f = open('file path', 'r')
 4 print(f.read())
 5 f.close()
 6 
 7 #第二种方法:
 8 try:
 9     f = open('file path', 'r')
10     print(f.read())
11 finally:
12     if f:
13         f.close()
14 
15 #第三种方法(简易):
16 with open('file path', 'r') as f:
17     print(f.read())
18 
19 
20 f=open('file path', 'r')
21 for line in f.readline():
22     print(line.strip())
23 f.close()
24 
25 
26 #写文件
27 #写入字符串:
28 input = open('file path', 'w')
29 input.write(str)
30 input.close()
31 
32 追加模式参数为'a'
33 写入二进制模式'wb'
34  
相关文章
相关标签/搜索