1、文件模式
b模式:控制文件读写的内容的模式
t模式:
1、读写都是以字符串(unicode)为单位
2、只针对文本文件
3、必须指定字符编码,即必须指定encodeing参数
b: binary模式
1、读写都是以bytes为单位
2、可以针对所有文件
3、一定不能指定的字符编码,即一定不能指定encodeing参数
错误演示:t模式只能读文本文件
with open(r'001.mp4',mode='rt') as f:
f.read() # 硬盘的二进制读入内存-》t模式会将读入内存的内容进行decode解码操作
with open(r'a.text',mode='rb') as f:
res=f.read() # utf-8的二进制
print(res,type(res))
print(res.decode('utf-8'))
with open(r'a.text',mode='rt',encoding='utf-8') as f:
res=f.read() # utf-8的二进制->unicode
print(res)
with open(r'a.text',mode='wb') as f:
f.write('你好heello'.encode('utf-8'))
文件拷贝工具
str_fule=input('源文件路径>>: ').strip()
dst_fule=input('源文件路径>>: ').strip()
with open(r'{}'.format(src_file),mode='rb') as f,\
open(r'{}'.format(dst_fule),mode='wb') as f1:
res=f.read() # 内存占用过大
f2.write(res)
for line in f:
f2.write(line)
循环读取文件
# 方式一:自己控制每次读取的数据的数据量
with open(r'a.text',mode='rb') as f:
while True:
res=f.read(1024)
if len(res) == 0:
break
print(len(res))
# 方式二:以单位读,当一行内容过长时一次性读入内容的数据量过大
with open(r'a.text',mode='rt',encoding='utf-8') as f:
for line in f:
print(line)
with open(r'a.text',mode='rb') as f:
for line in f:
print(line)
# 1、readline:一次读一行
with open(r'a.text',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)
# 2、readlines:
with open(r'a.text',mode='rt',encoding='utf-8') as f:
res=f.readlines()
print(res)
# f.read()与f.readlines()都是将内容一次性读入内容,如果内容过大会导致内存溢出,若还想将内容全读入内存,则必须分多次读入
2、写相关操作
# f.writelines:
with open('n.txt',mode='wt',encoding='utf-8') as f:
# f.write('asidgad\nasfd\n456')
l=['111\n','222','333']
# for line in l:
# f.write(line)
f.writelines(l)
with open('n.txt',mode='wt',encoding='utf-8') as f:
l=['111\n','222','333']
f.writelines(l)
# 补充1:如果时纯英文字符串,可以直接加前缀b得到bytes类型
l =[
b'111aaa1\n',
b'2qdad22',
b'33d33'
]
# 补充2:'上'.encode('utf-8') 等同于bytes('上',encoding='utf-8')
l = [
bytes('上啊',encoding='utf-8'),
bytes('吃吧就睡',encoding='utf-8'),
bytes('小辣椒',encoding='utf-8')
]
f.writelines(l)
# 3、flush:
with open('d.txt',mode='wt',encoding='utf-8') as f:
f.write('哈哈哈')
f.flush() # 只出现在测试系统
# 4、了解
with open('f.txt',mode='wt',encoding='utf-8') as f:
print(f.readable())
print(f.writable())
print(f.encoding)
print(f.name)
print(f.closed)
3、文件的高级操作:控制文件指针的移动
# 指针移动的单位都是以bytes/字节为单位
# 只有一种清空特殊:
# t模式下的read(n),n代表的字符个数
with open('a.text',mode='rt',encoding='utf-8') as f:
res=f.read(4)
print(res)
# f.seek(n,模式):n指的是自动的字节个数
# 模式:
# 模式:0:参照物是文件开头位置
f.seek(9,0)
f.seek(3,0) # 3
# 模式:1: 参照物是当前指针所在位置
f.seek(9,1)
f.seek(3,1) # 12
# 模式:2: 参照物是文件末尾位置,应该倒着移动
f.seek(-9,2) # 3
f.seek(-3,2) # 9
# 强调:只有0模式可以在t下使用,1,2必须在b模式下使用
# f.tell() # 获取文件指针当前位置
示范
with open('a.text',mode='rb') as f:
f.seek(9,0)
f.seek(3,0) # 3
# print(f.tell())
res=f.read()
print(res.decode('utf-8'))
with open('a.text',mode='rb') as f:
f.seek(9,1)
f.seek(3,1) # 12
print(f.tell())
with open('a.text',mode='rb') as f:
f.seek(-9,2)
print(f.tell())
f.seek(-3,2)
print(f.tell())
print(f.read().decode('utf-8'))
4、bytes
bytes=>二进制
得到bytes类型的三种方式
1、字符串编码之后的结果
'上'.encode('utf-8')
bytes('上',encoding='utf-8')
2、b必须是纯英文字符
3、b模式下打开文件,f.read()读书的内容
with open('acc.log',mode='at',encoding='utf-8') as f:
f.write('202033423432423jns士大夫w\n')
import time
with open('acc.log',mode='rt',encoding='utf-8') as f:
# 1、将指针跳到文件末尾
# f.read() # 错误
f.seek(0,2)
while True:
line=f.readline()
if len(line) == 0:
time.sleep(0.3)
else:
print(line)
5、replace替代
# 方式一:文本编辑采用的就是这种方式
with open('a.text',mode='rt',encoding='utf-8') as f:
res=f.read()
# print(res)
# data = res.replace('yay','is')
with open('a.text',mode='wt',encoding='utf-8') as f1:
# f1.write(data)
f1.write(res.replace('yay','is'))
# print(data)
# 方式二:
import os
with open('a.text',mode='rt',encoding='utf-8') as f,\
open('a.txt',mode='wt',encoding='utf-8') as f1:
for line in f:
f1.write(line.replace('ad','how'))
os.remove('a.text')
os.remove('a.txt')
with open('db.txt',mode='rt',encoding='utf-8') as f:
data=f.read()
with open('db.txt',mode='wt',encoding='utf-8') as f:
f.write(data.replace('kevin','SB'))