x模式
X:只写模式,不可读;不存在则创建,存在则报错
# with open('a.txt',mode='x',encoding='utf-8') as f:
# pass
# with open('c.txt',mode='x',encoding='utf-8') as f:
# f.read()
# with open('d.txt', mode='x', encoding='utf-8') as f:
# f.write('哈哈哈
') # x, 只写模式【不可读;不存在则创建,存在则报错】
b模式:binary模式
b模式和t模式的差别
t:
1、读写都是以字符串(unicode)为单位
2、只能针对文本文件
3、必须指定字符编码,即必须指定encoding参数
b:binary模式
1、读写都是以bytes为单位
2、可以针对所有文件
3、一定不能指定字符编码,即一定不能指定encoding参数
b模式的w、r应用以及错误应用
# 错误演示:t模式只能读文本文件
with open(r'爱nmlgb的爱情.mp4',mode='rt') as f:
f.read() # 硬盘的二进制读入内存-》t模式会将读入内存的内容进行decode解码操作
# 报错
# 因为有encoding指定字符编码 报错
with open(r'test.jpg',mode='rb',encoding='utf-8') as f:
res=f.read() # 硬盘的二进制读入内存—>b模式下,不做任何转换,直接读入内存
print(res) # bytes类型—》当成二进制
print(type(res))
with open(r'd.txt',mode='rb') as f:
res=f.read() # utf-8的二进制
print(res,type(res))
print(res.decode('utf-8'))
with open(r'd.txt',mode='rt',encoding='utf-8') as f:
res=f.read() # utf-8的二进制->unicode
print(res)
with open(r'e.txt',mode='wb') as f:
f.write('你好hello'.encode('gbk'))
with open(r'f.txt',mode='wb') 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') as f1,
open(r'{}'.format(dst_file),mode='wb') as f2:
# res=f1.read() # 会导致内存占用过大
# f2.write(res)
for line in f1:
f2.write(line)
循环读取文件
方式一:自己控制每次读取的数据的数据量
with open(r'test.jpg',mode='rb') as f:
while True:
res=f.read(1024) # 1024 这里自己控制每次读取1024
if len(res) == 0: # 长度只剩0是返回 要不会无限循环
break
print(len(res),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(len(line),line)
with open(r'test.jpg',mode='rb') as f:
for line in f:
print(len(line),line)
文件的操作
读相关操作
readline:一次读一行
with open(r'g.txt',mode='rt',encoding='utf-8') as f:
# res1=f.readline() # readline(n) n可以设置读取一行内的长度 如果未满一行长度 res2读取该行剩下的内容
# res2=f.readline()
# print(res2)
# 循环将每行内容一次读出来
while True:
line=f.readline()
if len(line) == 0:
break
print(line)
readlines: #得到一个大列表 列表里放每行的内容
with open(r'g.txt',mode='rt',encoding='utf-8') as f:
res=f.readlines()
print(res)
强调
f.read()与f.readlines()都是将内容一次性读入内存,如果内容过大会导致内存溢出,若还想将内容全读入内存,则必须分多次读入,for、while
写相关操作
f.writelines(): # 将列表里循环取值写入文件中
t模式:
with open('g.txt',mode='wt',encoding='utf-8') as f:
# f.write('1111
222
3333
')
# l=['11111
','2222','3333',4444]
l=['11111
','2222','3333']
# for line in l:
# f.write(line)
f.writelines(l)
b模式
with open('g.txt', mode='wb') as f:
# l = [
# '1111aaa1
'.encode('utf-8'),
# '222bb2'.encode('utf-8'),
# '33eee33'.encode('utf-8')
# ]
# 补充1:如果是纯英文字符,可以直接加前缀b得到bytes类型
# l = [
# b'1111aaa1
',
# b'222bb2',
# b'33eee33'
# ]
# 补充2:'上'.encode('utf-8') 等同于bytes('上',encoding='utf-8')
#l = [
# '上
'.encode('utf-8'),
# bytes('上啊',encoding='utRf-8'),
# bytes('冲呀',encoding='utf-8'),
# bytes('小垃圾们',encoding='utf-8'),
#]
f.writelines(l)
拓展(flush、readable、writable、encoding、name、closed)
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) # 判断encoding编码 如果文件打开模式为b,则没有该属性
print(f.name) # 文件名称
print(f.closed)# 文件是否关闭
# False
# True
# utf-8
# h.txt
# True
seek(指针移动)
简介
指针移动的单位都是以bytes/字节为单位
只有一种情况特殊:t模式下的read(n),n代表的是字符个数
文件内指针的移动都是Bytes为单位的,唯一例外的是t模式下的read(n),n以字符为单位
# 文件内容‘abc你好’
with open('aaa.txt',mode='rt',encoding='utf-8') as f:
res=f.read(6)
print(res)
# abc你好
f.seek(n,模式)n指的是移动的字节个数 0,1,2模式
模式:
0: 默认的模式,该模式代表指针移动的字节数是以文件开头为参照的
1: 该模式代表指针移动的字节数是以当前所在的位置为参照的
2: 该模式代表指针移动的字节数是以文件末尾的位置为参照的
强调:其中0模式可以在t或者b模式使用,而1跟2模式只能在b模式下用
模式0:参照物是文件开头位置
# a.txt文件中为abc你好
with open('a.txt',mode='rt',encoding='utf-8') as f:
f.seek(3,0) # 参照文件开头移动了3个字节
print(f.tell()) # 查看当前文件指针距离文件开头的位置,输出结果为3
print(f.read()) # 从第3个字节的位置读到文件末尾,输出结果为:你好
# 由于在t模式下,会将读取的内容自动解码,所以必须保证读取的内容是一个完整中文数据,否则解码失败。 一个中文有三个字节
模式1:参照物是当前指针所在位置
# a.txt文件中为abc你好
with open('a.txt',mode='rb') as f:
f.seek(3,1) # 从当前位置往后移动3个字节,而此时的当前位置就是文件开头
print(f.tell()) # 输出结果为:3
# print(f.read().decode('utf-8')) # 加上这句话的话 因为读的时候读出了'你好'六个字节 所以下面是从9开始往后移动
f.seek(4,1) # 从当前位置往后移动4个字节,而此时的当前位置为3
print(f.tell()) # 输出结果为:7
模式2:参照物是文件末尾位置,应该倒着移动
with open('a.txt',mode='rb') as f:
f.seek(0,2) # 参照文件末尾移动0个字节, 即直接跳到文件末尾
print(f.tell()) # 输出结果为:9
f.seek(-3,2) # 参照文件末尾往前移动了3个字节
print(f.read().decode('utf-8')) # 输出结果为:好
seek的小应用——实现动态查看最新一条日志的效果
tail -f access.log程序实现
import time
with open('access.log',mode='rb') as f:
f.seek(0,2)
while True:
line=f.readline()
if len(line) == 0:
# 没有内容
time.sleep(5) #延迟五秒
else:
print(line.decode('utf-8'),end='')