我编写了一个python脚本,其中包含一个睡眠循环以观看日志文件,等待换行处理:
file = open('logFile.log', 'r')
while 1:
where = file.tell()
line = file.readline()
if not line:
time.sleep(1)
file.seek(where)
else:
print line, # already has newline
我想对此进行修改,以便每隔1个小时以上没有换行,结束循环并继续执行脚本。到目前为止,这样做还没有成功。
最佳答案
此问题的重要一点是连续1个小时没有新行,因此在新行打印后必须重置计时器。
count = 0
one_hour = 60 * 60
file = open('logFile.log', 'r')
while 1:
where = file.tell()
line = file.readline()
if not line:
count = count + 1
if count >= one_hour:
break
time.sleep(1)
file.seek(where)
else:
print line
count = 0 # Here needs to reset the time couter!