1. 文件操作基础
(1) 文件路径
绝对路径: 是从盘符开始的路径, 形如 C:\windows\\cmd.exe
相对路径: 是从当前路径开始的路径, 假如当前路径为 C:\windows, 要描述上述路径, 只需输入 \ cmd.exe.
实际上, 严格的相对路径写法应为.\\cmd.exe. 其中,. 表示当前路径, 在通道情况下可以省略, 只有在特殊的情况下不能省略.
(2) 编码方式
以什么编码方式储存的文件, 就以什么方式打开
(3) 操作方式
只读, 只写, 追加, 读写 [r+, 最常用], 写读...
2. 操作方式详解
bytes 用于查看非文字类的文件, 上传下载储存文件
下面所有例子都由相对路径来描述:
(1) 只读: r rb
- # 正常: r 只读
- print('-------- 相对路径: 只读 ----------')
- f = open('文件',mode='r',encoding='utf-8') #文件: 你好呀
- content = f.read()
- print(content) #你好呀
- f.close()
- #bytes:rb
- print('-------- 相对路径: 只读 ----------')
- f = open('文件',mode='rb')
- content = f.read()
- print(content) #b'\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x91\x80'
- f.close()
(2) 只写: w wb
- # 正常: w 只写
- # 文件不存在, 则创建完了再写
- print('-------- 相对路径: 只写 ----------')
- f = open('file1',mode='w',encoding='utf-8') #file1 不存在
- f.write("halou") #halou
- f.close()
- # 文件存在, 先删除原文件内容, 再写
- f = open('file1',mode='w',encoding='utf-8') #file 存在, 内容: halou
- f.write("辛辰辰") #辛辰辰
- f.close()
- #bytes:wb
- # 文件不存在, 则创建完了再写
- print('-------- 相对路径: 只写 ----------')
- f = open('file2',mode='wb')
- f.write("哈喽 halou123".encode('utf-8')) #需要对写的内容进行编码 encode, 文件内容: 哈喽 halou123
- f.close()
- # 文件存在, 先删除原文件内容, 再写
- f = open('file2',mode='w',encoding='utf-8') #file 存在, 内容: halou
- f.write("辛辰辰") #辛辰辰
- f.close()
(3) 追加: a ab
- # 正常: a 追加
- print('-------- 相对路径: 追加 ----------')
- f = open('file1',mode='a',encoding='utf-8') #file1: 辛辰辰
- f.write('追加追加') #辛辰辰追加追加
- f.close()
- #bytes:ab
- print('-------- 相对路径: 追加 ----------')
- f = open('file2',mode='ab') #file2: 辛辰辰
- f.write('zai 追加'.encode('utf-8')) #辛辰辰 zai 追加
- f.close()
(4) 读写: r+ r+b
- # 正常: r+ 读写 [最常用]
- print('-------- 相对路径: 读写 ----------')
- f = open('file',mode='r+',encoding='utf-8') #file:python9
- print(f.read()) #python9
- f.write('辛辰辰') #python9 辛辰辰
- f.close()
- print('-------- 相对路径: 写读 ----------')
- f = open('file',mode='r+',encoding='utf-8') #file:python9
- f.write('xcc') #file:xcchon9
- print(f.read()) #hon9
- f.close()
- #bytes:r+b
- print('-------- 相对路径: 读写 ----------')
- f = open('file',mode='r+b') #file:python9
- print(f.read()) #b'python9'
- f.write('辛辰辰'.encode('utf-8')) #file:python9 辛辰辰
- f.close()
- print('-------- 相对路径: 写读 ----------')
- f = open('file',mode='r+b') #file:python9
- f.write('xcc'.encode('utf-8')) #file:xcchon9
- print(f.read()) #b'hon9'
- f.close()
(5) 写读: w+ w+b
- # 正常: w+ 写读 (先清除, 再写)
- print('-------- 相对路径: 写读 ----------')
- f = open('file',mode='w+',encoding='utf-8') #file:python9
- f.write('辛辰辰') #辛辰辰
- print(f.read()) #空
- f.close()
- #bytes:w+b
- print('-------- 相对路径: 写读 ----------')
- f = open('file',mode='w+b') #file:python9
- f.write('辛辰辰'.encode('utf-8')) #辛辰辰
- print(f.read()) #b''
- f.close()
(6) 追加: a+ a+b
- # 正常: a+ 追加
- print('-------- 相对路径: 追加 ----------')
- f = open('file',mode='a+',encoding='utf-8') #file:python9
- f.write('辛辰辰') #python9 辛辰辰
- print(f.read()) #空
- f.seek(0) #调整光标位置到开始的地方
- print(f.read()) #python9 辛辰辰
- f.close()
- #bytes:a+b
- print('-------- 相对路径: 追加 ----------')
- f = open('file',mode='a+b') #file:python9
- f.write('辛辰辰'.encode('utf-8')) #python9 辛辰辰
- print(f.read()) #b''
- f.seek(0) #调整光标位置到开始的地方
- print(f.read()) #b'python9\xe8\xbe\x9b\xe8\xbe\xb0\xe8\xbe\xb0'
- f.close()
3. 功能详解
(1)read
读出来的都是字符, 一个字母 / 汉字
- print('-------- 功能详解 ----------')
- # f = open('file',mode='r+',encoding='utf-8') #file:pythonpythonpython
- f = open('file',mode='r+',encoding='utf-8') #file: 你好你好你好
- content = f.read(3) #read 读出来的都是字符, 一个字母 / 汉字
- # print(content) #pyt
- print(content) #你好你
- f.close()
- (2)seek
按照字节定光标的位置 (一个汉字 3 字节, 一个英文字母 1 字节)
- print('-------- 功能详解 ----------')
- # f = open('file',mode='r+',encoding='utf-8') #file:PYthonpyTHonpythON
- f = open('file',mode='r+',encoding='utf-8') #file: 你好呀辛辰
- f.seek(3) #光标: 按照字节定位置
- content = f.read()
- # print(content) #honpyTHonpythON: 光标位置再三个字节之后 (utf-8 中: 一个字母一个字节)
- print(content) #好呀辛辰: 光标位置再三个字节之后 (utf-8 中: 一个汉字三个字节)
- f.close()
- (3)tell
监测光标的位置
- print('-------- 功能详解 ----------')
- f = open('file',mode='r+',encoding='utf-8') #file:PYthonpyTHonpythON
- # f = open('file',mode='r+',encoding='utf-8') #file: 你好呀辛辰
- f.seek(3)
- f.tell() #监测光标的位置
- print(f.tell()) #3
- # print(f.read()) #从第三个字节后面开始读取: 好呀辛辰
- print(f.read()) #从第三个字节后面开始读取: honpyTHonpythON
- f.close()
应用举例:
- f = open('file',mode='a+',encoding='utf-8') #file:python9
- f.write('辛辰辰') #python9 辛辰辰
- count = f.tell()
- f.seek(f.tell()-3)
- print(f.read()) #辰 (后三个字节表示一个汉字)
- f.close()
- # 读取文件 file 中的从第 3 个字节开始的连续 5 个字节的内容
- f = open('file',mode='a+',encoding='utf-8') #file:python9 辛辰辰
- f.seek(2)
- print(f.read(5)) #thon9(python9 辛辰辰)
- f.close()
- (4)with open
使用 with open 打开文件, 可以不用写 f.close()
- # 打开一个文件
- with open('file',mode='r+',encoding='utf-8') as f:
- print(f.read()) #这是 file1 文件
- # 打开多个文件
- with open('file1',mode='r+',encoding='utf-8') as f1,open('file2',mode='w+',encoding='utf-8') as f2:
- print(f1.read()) #这是 file1 文件
- f2.write('哈喽') #file2: 哈喽
- print(f2.read()) #空: w + 是先清除再写
- (5)others
- f = open('file',mode='r+',encoding='utf-8')
- f.readable() #是否可读
- print(f.readable()) #True
- line = f.readline() #读一行
- print(line) #这是 file 文件 1
- lines = f.readlines() #把每一行当成列表中的一个元素, 添加到 list 中
- print(lines) #['这是 file 文件 2\n', '这是 file 文件 3\n', '这是 file 文件 4\n', '这是 file 文件 5']
- # f.truncate(5) #截取前五位
- f.close()
来源: http://www.bubuko.com/infodetail-2769393.html