1. file 写 (.......w)
- f1 = file(/test/test.txt,w) #w 开启写操作
- f1.write(the line is first.\n) #write, 写入
- f1.flush() #刷到硬盘
2.file 读 (.......r)
- f1 = file(/test/test.txt,w) #r 开启读操作
- f1.read() #read , 读文件
3.file 追加 (.......a)
- f1 = file(/test/test1.txt,a) #a 开启追加操作
- f1.write(the line is second.\n)
- f1.flush()
4.file 退出缓存 (close)
f1.close()
5.file 读写模式 (.......r+) #不常用
- f1 = file(test1.txt,r+)
- f1.write(the change for file test.\n)
- f1.flush()
- # 这时查看 test1.txt 发现文件第二行被替换了, 读写模式
6.file 写读模式 (.......w+) #不常用
# 自己测试
7.file 跨平台二进制转换 (........rb 或者 wb)
- # 类似 windows 上某些文件传到 linux 上, 读行时, 发现很多不一样的字符串, 我们经常在 linux 上使用 dos2unix 来处理该文件, 在 python 中 rb/wb 读转换
- f1 = file(test1.txt,rb)
来源: http://www.bubuko.com/infodetail-2542863.html