标签:enc 打印 seek open trunc 修改 ant lse UNC
#实时打印输出日志文件
import time
with open(‘access.log‘, ‘rb‘) as f:
f.seek(0, 2)
while True:
line = f.readline()
if line:
print(line.decode(‘utf-8‘))
else:
time.sleep(0.05)
# #截断文件
with open(‘test.txt‘,‘a‘,encoding=‘utf-8‘) as f1:
f1.truncate(3)
# 修改文件内容
import os
with open(‘test.txt‘,‘r‘,encoding=‘utf-8‘) as read_file,open(‘.test.txt.swap‘,‘w‘,encoding=‘utf-8‘) as write_file:
for line in read_file:
if ‘anthony‘ in line:
line = line.replace(‘anthony‘,‘nb‘)
write_file.write(line)
os.remove(‘test.txt‘)
os.rename(‘.test.txt.swap‘,‘test.txt‘)
标签:enc 打印 seek open trunc 修改 ant lse UNC
原文地址:https://www.cnblogs.com/ipyanthony/p/9088373.html