1.了解:x模式
①x: 只写模式
②不可读;
③不存在则创建,存在则报错;
with open('a.txt',mode='x',encoding='utf-8') as f:
pass # 没有a.txt,即创建一个a.txt
with open('c.txt',mode='x',encoding='utf-8') as f:
f.read() # 没有c.txt,即创建一个c.txt,但是不支持读
# 报错:io.UnsupportedOperation: not readable
with open('d.txt', mode='x', encoding='utf-8') as f:
f.write('哈哈哈\n') #没有d.txt,即创建一个d.txt,并且写入信息“哈哈哈”
2.控制文件读写内容的“t模”和“b模式”
t模式:
1、读写都是以字符串(unicode)为单位
2、只能针对文本文件
3、必须指定字符编码,即必须指定encoding参数
b:binary模式
1、读写都是以bytes为单位
2、可以针对所有文件
3、一定不能指定字符编码,即一定不能指定encoding参数
总结:
1、在操作纯文本文件方面t模式帮我们省去了编码与解码的环节,b模式则需要手动编码与解码,所以此时t模式更为方便
2、针对非文本文件(如图片、视频、音频等)只能使用b模式
案例1: 错误演示,t模式只能读文本文件
with open(r'xxx.mp4',mode='rt') as f:
f.read() # 硬盘的二进制读入内存-》t模式会将读入内存的内容进行decode解码操作
案例2:错误示例,b模式不能指定encoding参数
with open(r'desktop.png',mode='rb',encoding='utf-8') as f:
res=f.read()
print(res)
print(type(res))
案例3:读取图片信息
with open(r'desktop.png',mode='rb') as f:
res=f.read() # 硬盘的二进制读入内存—>b模式下,不做任何转换,直接读入内存
print(res) # bytes类型—》当成二进制
print(type(res)) # 结果:<class 'bytes'>
注1:读取的内容均为bytes类型的字节;
注2:每行内容的结尾均有回车 /n;
下图为输出结果:
案例4:b模式读取文本信息
with open(r'd.txt',mode='rb') as f:
res=f.read() # utf-8的二进制
print(res,type(res))
print(res.decode('utf-8')) # 从硬盘读取到内存,解码;
#存文件信息的时候编码格式为utf-8,故解码时也应为utf-8
案例5:t模式读取文本内容
# d.txt的内容为:你好呀大飞飞
with open(r'd.txt',mode='rt',encoding='utf-8') as f:
res=f.read() # utf-8的二进制->unicode
print(res) # 结果:你好呀大飞飞
案例6:b模式写文件且编码为’gbk’
with open(r'e.txt',mode='wb') as f:
f.write('你好hello'.encode('gbk'))
**备注:**写入文本的内容乱码,因为指定了写入到硬盘的格式为’gbk‘(pycharm默认的编码),而读e,txt时编码却为utf-8(pycharm打开文件,即为读文件),由于读和存的编码不一致,所以导致乱码。
案例7:分别以utf-8和gbk编码格式写入文件
with open(r'f.txt',mode='wb') as f:
f.write('你好hello\n'.encode('utf-8'))
f.write('哈哈哈'.encode('gbk'))
3.文件拷贝工具
1.复制文件时可能时文件、图片、视频等,使用b模式复制更为准确;
2.如果需要复制的源文件内容过大,f.read()一次性读取完内容时存在内存泄露的风险;
3.针对第2点,可以使用readline的方式一行行读取内容,解决读取时内存占用过大的风险;
4.针对第3点,可能存在一行的内容过大,故最好的方式是指定字节个数进行读取;
'''示例:一次性读取文件内容'''
src_file=input('源文件路径>>: ').strip()
dst_file=input('源文件路径>>: ').strip()
with open(r'{}'.format(src_file),mode='rb') as f1,\
open(r'{}'.format(dst_file),mode='wb') as f2:
#res=f1.read() # 内存占用过大,不推荐使用
#f2.write(res)
for line in f1:
f2.write(line)
4. 循环读取文件
方式一:自己控制每次读取的数据的数据量
with open(r'test.jpg',mode='rb') as f:
while True:
res=f.read(1024) # 指定每次读取的字节上线,1024
if len(res) == 0:
break
print(len(res))
方式二:以行为单位读,当一行内容过长时会导致一次性读入内容的数据量过大
with open(r'g.txt',mode='rt',encoding='utf-8') as f:
for line in f:
print(len(line),line)
with open(r'g.txt',mode='rb') as f:
for line in f:
print(line)
with open(r'test.jpg',mode='rb') as f:
for line in f:
print(line)