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 >>> 

Python之文件操作

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

文件对象方法:

Python之文件操作

 

seek()的选项:

Python之文件操作

 

相关文章:

  • 2021-07-22
  • 2021-12-20
猜你喜欢
  • 2022-12-23
  • 2021-03-31
相关资源
相似解决方案