日常练习 1.0

#实时打印输出日志文件
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')


猜你喜欢

转载自www.cnblogs.com/ipyanthony/p/9088373.html
今日推荐