我使用写函数将数据保存到文本文件(data.txt)中,当信息传入时,它将以下格式保存:
127682
127689
127696
127696
127696
127694
127693
127693
127694
127713
127713
127713
127713
127713
127713
127706
127706
127703
我该如何删除,比方说,每26分钟后删除前7行?
发布于 2018-07-25 11:34:42
你可以这样实现它:
import time
started = time.time()
while True:
if time.time() - 1560 > started: # 1560 = 26 minutes
with open('target_file.txt', 'r') as fr:
lines = fr.readlines()
with open('target_file.txt', 'w') as fw:
fw.writelines(lines[7:])
started = time.time()
time.sleep(1)
你还可以使用threading.Thread类在主程序中将其作为单独的守护程序线程运行
https://stackoverflow.com/questions/-100005719
复制相似问题