我正在尝试浏览日志文件,但此日志文件不断增长。我不想每次都从头开始阅读。有一种方法可以用python做到这一点,但是......
import re
def cogeUltimPosLogFile():
with open('/tmp/lastPositionLogFile', 'r') as lf:
content = lf.read()
return int(content)
def marcaUltimPosLogFile(lpos):
with open('/tmp/lastPositionLogFile', 'w') as lf:
lf.write(str(lpos))
def log_browser(patron2agarar):
lmatch=[]
ln=0
nbChar=0
with open("camucamu.log", 'r') as contenilog:
previousEndOfFile=cogeUltimPosLogFile()
contenilog.seek(previousEndOfFile)
for linea in contenilog:
m = re.search(r"{}".format(patron2agarar), linea)
if m:
lmatch.append([ln, m.group(1)])
nbChar=nbChar+len(linea)
ln=ln+1
setLastPositionLogFile(previousEndOfFile+nbChar)
return lmatch
print(log_parser("(.*error.*)"))
但是如果重新初始化了获得偏移量的文件,或者第一次运行python脚本会怎样?