1. 文件处理模式
r 以读的方式打开
w 以写的方式打开
a 以追加的方式打开
r+ 以读写方式打开
w + 以读写方式打开
a + 以读写方式打开
rb 以二进制读的方式打开
wb 以二进制写的方式打开
ab 以二进制追加的方式打开
rb+ 以二进制读写的方式打开
wb + 以二进制读写的方式打开
ab + 以二进制读写的方式打开
- f=open('新建文本文件. txt','r',encoding='utf-8')
- for line in f:
- print(line)
- f.close()
- with open('新建文本文件. txt','r',encoding='utf-8')
- for line in f:
- print(line)
2. 以 w 和 a 模式处理文件的区别
- with open('新建文本文件. txt','w',encoding='utf-8')
- with open('新建文本文件. txt','a',encoding='utf-8')
w 模式会覆盖之前的文件, a 模式不会覆盖之前的文件, 而是追加写在文件末尾
来源: http://www.bubuko.com/infodetail-2650179.html