【发布时间】:2021-02-14 08:05:23
【问题描述】:
我目前正在编写一个不和谐的机器人,它读取一个不断更新的本地日志文件。每次添加新行并匹配特定模式时,机器人(应该)发布包含该内容的消息。我尝试了以下或我目前的立场是以下代码。我的代码当前的问题是文件一直打开。如果自上次阅读后更新文件,有没有办法只读取文件?或者换句话说,有没有一种方法可以实现我的解决方案,该解决方案不需要一直打开文件/一直忙于读取文件?
while not BOT.is_closed():
for log in logs:
file_path = config["path_to_log"]
logfiles= []
for filename in glob.glob(os.path.join(file_path,f'_{log}*')):
logfiles.append(filename)
latest_file = max(logfiles, key=os.path.getmtime)
""" #second try doenst work if to much content gets added at the same time
with open(latest_file,"rb") as f:
if(len(messagelist)>1000):
messagelist = []
f.seek(-2, os.SEEK_END)
while f.read(1) != b'\n':
f.seek(-2, os.SEEK_CUR)
last_line = f.readline().decode()
for word in words:
if word in last_line and last_line not in messagelist:
messagelist.append(last_line)
print(last_line)
await channel.send(last_line)
else:
time.sleep(1)
"""
# first try it works but i dont think its the best solution
try:
fp = open(latest_file, 'r')
except:
end_program("Error while reading the log file")
for line in (fp.readlines() [-10:]):
new = line
for word in words:
if word in new and new not in messagelist:
messagelist.append(new)
print(new)
await channel.send(new)
else:
time.sleep(1)
###
fp.close()
【问题讨论】:
-
github.com/kasun/python-tail 也可能有帮助。
标签: python-3.x file logging