# 0模式详解
# with open('a.txt',mode='rt',encoding='utf-8') as f:
# f.seek(4,0)
# print(f.tell())
# print(f.read())
# with open('a.txt',mode='rb') as f:
# # f.seek(4,0)
# f.seek(2,0)
# print(f.tell())
# print(f.read().decode('utf-8'))
# with open('a.txt',mode='rt',encoding='utf-8') as f:
# f.seek(5,0)
# print(f.read())
# 1模式详解
# with open('a.txt',mode='rb') as f:
# f.seek(3,1)
# print(f.tell())
# f.seek(4,1)
# print(f.tell())
# print(f.read().decode('utf-8'))
# 2模式详解
# with open('a.txt',mode='rb') as f:
# f.seek(-9,2)
# data=f.read()
# print(data.decode('utf-8'))
# tail -f access.log
with open('access.log',mode='rb') as f:
f.seek(0,2)
while True:
line=f.readline()
if len(line) == 0:
# 没有内容
continue
else:
print(line.decode('utf-8'),end='')
with open('a.txt','r+t',encoding='utf-8') as f:
f.seek(4,0)
print(f.tell())
f.write('我擦嘞')
# 1. 将文件内容发一次性全部读入内存,然后在内存中修改完毕后再覆盖写回原文件
# 优点: 在文件修改过程中同一份数据只有一份
# 缺点: 会过多地占用内存
# 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'))
# 2. 以读的方式打开原文件,以写的方式打开一个临时文件,一行行读取原文件内容,修改完后写入临时文件...,删掉原文件,将临时文件重命名原文件名
# 优点: 不会占用过多的内存
# 缺点: 在文件修改过程中同一份数据存了两份
import os
with open('db.txt',mode='rt',encoding='utf-8') as read_f,\
open('.db.txt.swap',mode='wt',encoding='utf-8') as wrife_f:
for line in read_f:
wrife_f.write(line.replace('SB','kevin'))
os.remove('db.txt')
os.rename('.db.txt.swap','db.txt')