#实时打印输出日志文件 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') # 禁止显示非法搜索内容
li = ['民谣','电音','纯音乐','古风'] l1 = [] comment = input('enter the world,please:').strip() for i in li: if i in comment: comment = i.replace(i,'*'*len(i)) l1.append(comment) print(*l1)