Python之文件操作

file_handler=open(filename,,mode)

打开文件的两种方式,file('filename')和open('filename'):

 1 >>> f1 = file('test.c')
 2 >>> f1.read()
 3 'hello branches\n'
 4 >>> f1.close()
 5 >>> 
 6 >>> f1 = open('test.c')
 7 >>> f1
 8 <open file 'test.c', mode 'r' at 0x8a59f40>
 9 >>> f1.read()
10 'hello branches\n'
11 >>> 
12 >>> f1.write('good neight')
13 Traceback (most recent call last):
14   File "<stdin>", line 1, in <module>
15 IOError: File not open for writing
16 >>> 
17 >>> f1.close()
18 >>> 

 注意'a'和'r+'的区别,'a'是在文件末尾追加,'r+'是在当前文件位置指针(fseek)处追加

文件对象方法:

 

seek()的选项:

 

posted on 2017-06-12 20:25  枝桠  阅读(146)  评论(0编辑  收藏  举报

导航