Python正课26 —— 文件处理的补充
本文内容皆为作者原创,如需转载,请注明出处:https://www.cnblogs.com/xuexianqi/p/12503333.html
一:x模式
x,只写模式(不可读;不存在则创建,存在则报错)
a.txt存在,就会报错
with open(r'a.txt',mode='x',encoding='UTF-8') as f:
f.read()
FileExistsError: [Errno 17] File exists: 'a.txt'
b.txt不存在,就会创建
with open(r'b.txt',mode='x',encoding='UTF-8') as f:
f.write('haha\n')
二:b模式:binary模式
硬盘的二进制读入内存 --> t模式会将读入内存的内容进行decode解码
with open(r'预见未来.rmvb',mode='rt') as f:
f.read() # 硬盘的二进制读入内存 --> t模式会将读入内存的内容进行decode解码
硬盘的二进制读入内存 --> b模式下,不做任何转换,直接读入内存
with open(r'预见未来.rmvb',mode='rb') as f:
res = f.read() # 硬盘的二进制读入内存 --> b模式下,不做任何转换,直接读入内存
print(res) # bytes类型--》当成二进制
# OverflowError: bytes object is too large to make repr
# 溢出错误:bytes对象太大,无法生成repr
with open(r'b.txt',mode='rb') as f:
res = f.read() # 硬盘的二进制读入内存 --> b模式下,不做任何转换,直接读入内存
print(res,type(res))
print(res.decode('utf-8'))
b.txt内容是:“哈哈哈”
输出b'\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88' 16进制显示
说明,一个中文字符,对应3个bytes
with open(r'b.txt',mode='rt',encoding='utf-8') as f:
res = f.read() # UTF-8的二进制--》unicode
print(res)
# print(res.decode('utf-8'))
下列操作是错误的,编码最好一致
with open(r'b.txt',mode='wt',encoding='utf-8') as f:
f.write('你好hello'.encode('utf-8'))
f.write('哈哈哈'.encode('gbk'))
文件拷贝工具
src_file = input('原文件路径:').strip()
dst_file = input('新文件路径:').strip()
with open(r'{}'.format(src_file),mode='rb',encoding='utf-8') as f1,\
open(r'{}'.format(dst_file),mode='wb',encoding='utf-8') as f2:
# res = f1.read() # 内存占用过大
# f2.write(res)
for line in f1:
f2.write(line)
循环读取文件:
图片、视频每行都很简短,多的用while,少的用for
方式1
with open(r'关机64.ico',mode='rb') as f:
while True:
res = f.read(1024) # 一行读取1024个字节
if not res:
break
print(len(res))
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
574
方式2
with open(r'a.txt',mode='rt',encoding='utf-8') as f:
for line in f:
print(line)
滚滚长江东逝
滚滚长江东
滚滚长江
滚滚长
滚滚
滚
Windows模式下,换行符是 "\r\n"
with open(r'a.txt',mode='rb') as f:
for line in f:
print(line)
b'\xe6\xbb\x9a\xe6\xbb\x9a\xe9\x95\xbf\xe6\xb1\x9f\xe4\xb8\x9c\xe9\x80\x9d\r\n'
b'\xe6\xbb\x9a\xe6\xbb\x9a\xe9\x95\xbf\xe6\xb1\x9f\xe4\xb8\x9c\r\n'
b'\xe6\xbb\x9a\xe6\xbb\x9a\xe9\x95\xbf\xe6\xb1\x9f\r\n'
b'\xe6\xbb\x9a\xe6\xbb\x9a\xe9\x95\xbf\r\n'
b'\xe6\xbb\x9a\xe6\xbb\x9a\r\n'
b'\xe6\xbb\x9a'
三:控制文件读写内容的模式
t:文本(默认的模式)
1.读写都是以str(Unicode)为单位的
2.只能针对文本文件
3.必须为open()指定encoding='utf-8'
b:二进制 / bytes
1.读写都是以bytes为单位的
2.可以针对所有文件
3.不能制定字符编码,即 不能指定encodeing参数
总结:
1.在操作纯文本文件方面t模式帮我们省去了编码与解码的环节,b模式则需要手动编码与解码,所以此时t模式更为方便
2.针对非文本文件(如图片、视频、音频等)只能使用b模式
四:操作文件的方法
读操作
f.read() # 读取所有内容,执行完该操作后,文件指针会移动到文件末尾
f.readline() # 一次读取一行内容,光标移动到第二行首部
with open(r'g.txt',mode='rt',encoding='utf-8') as f:
# res1=f.readline()
# res2=f.readline()
# print(res2)
while True:
line=f.readline()
if len(line) == 0:
break
print(line)
f.readlines() # 读取每一行内容,存放于列表中
with open(r'g.txt',mode='rt',encoding='utf-8') as f:
res=f.readlines()
print(res)
强调:
f.read()与f.readlines()都是将内容一次性读入内容,如果内容过大会导致内存溢出,若还想将内容全读入内存,则必须分多次读入,有两种实现方式:
# 方式一
with open('a.txt',mode='rt',encoding='utf-8') as f:
for line in f:
print(line) # 同一时刻只读入一行内容到内存中
# 方式二
with open('1.mp4',mode='rb') as f:
while True:
data=f.read(1024) # 同一时刻只读入1024个Bytes到内存中
if len(data) == 0:
break
print(data)
写操作
f.write('1111\n222\n') # 针对文本模式的写,需要自己写换行符
with open('h.txt',mode='wt',encoding='utf-8') as f:
# f.write('1111\n222\n3333\n')
# l=['11111\n','2222','3333',4444]
l=['11111\n','2222','3333']
# for line in l:
# f.write(line)
f.writelines(l)
f.write('1111\n222\n'.encode('utf-8')) # 针对b模式的写,需要自己写换行符
with open('h.txt',mode='wt',encoding='utf-8') as f:
l = ['111\n','222\n','333',444]
for line in l:
f.write(line)
#TypeError: write() argument must be str, not int
f.writelines(['333\n','444\n']) # 文件模式
f.writelines([bytes('333\n',encoding='utf-8'),'444\n'.encode('utf-8')]) #b模式
with open('h.txt', mode='wb') as f:
l = [
'1111aaa1\n'.encode('utf-8'),
'222bb2'.encode('utf-8'),
'33eee33'.encode('utf-8')
]
补充1:如果是纯英文字符,可以直接加前缀b得到bytes类型
with open('h.txt', mode='wb') as f:
l = [
b'1111aaa1\n',
b'222bb2',
b'33eee33'
]
补充2:'上'.encode('utf-8') 等同于bytes('上',encoding='utf-8')
with open('h.txt', mode='wb') as f:
l = [
bytes('上啊',encoding='utf-8'),
bytes('冲呀',encoding='utf-8'),
bytes('小垃圾们',encoding='utf-8'),
]
flush:
with open('h.txt', mode='wt',encoding='utf-8') as f:
f.write('哈')
# f.flush()
五:了解的知识点
with open('h.txt', mode='wt',encoding='utf-8') as f:
print(f.readable())
print(f.writable())
print(f.encoding)
print(f.name)
print(f.closed)