读取经常更新的文件
- 2025-02-11 09:50:00
- admin 原创
- 38
问题描述:
我目前正在 Linux 系统上用 Python 编写一个程序。目标是读取日志文件并在找到特定字符串时执行 bash 命令。另一个程序不断写入日志文件。
我的问题是:如果我使用该方法打开文件,open()
我的 Python 文件对象是否会随着实际文件被其他程序写入而更新,还是我必须在一定时间间隔内重新打开该文件?
更新:感谢到目前为止的回答。我可能应该提到该文件是由 Java EE 应用程序写入的,因此我无法控制何时将数据写入其中。我目前有一个程序,它每 10 秒重新打开文件一次,并尝试从文件中上次读取的字节位置读取。目前它只是打印出返回的字符串。我希望该文件不需要重新打开,但读取命令会以某种方式访问 Java 应用程序写入文件的数据。
#!/usr/bin/python
import time
fileBytePos = 0
while True:
inFile = open('./server.log','r')
inFile.seek(fileBytePos)
data = inFile.read()
print data
fileBytePos = inFile.tell()
print fileBytePos
inFile.close()
time.sleep(10)
感谢您对 pyinotify 和生成器的提示。我将研究这些内容以找到更好的解决方案。
解决方案 1:
我建议你看一下 David Beazley 的Python 生成器技巧,尤其是第 5 部分:处理无限数据。它将tail -f logfile
实时处理相当于 Python 的命令。
# follow.py
#
# Follow a file like tail -f.
import time
def follow(thefile):
thefile.seek(0,2)
while True:
line = thefile.readline()
if not line:
time.sleep(0.1)
continue
yield line
if __name__ == '__main__':
logfile = open("run/foo/access-log","r")
loglines = follow(logfile)
for line in loglines:
print line,
解决方案 2:
“一次互动胜过千言万语”
>>> f1 = open("bla.txt", "wt")
>>> f2 = open("bla.txt", "rt")
>>> f1.write("bleh")
>>> f2.read()
''
>>> f1.flush()
>>> f2.read()
'bleh'
>>> f1.write("blargh")
>>> f1.flush()
>>> f2.read()
'blargh'
换句话说 - 是的,一个“打开”就可以了。
解决方案 3:
这是Jeff Bauer答案的稍作修改的版本,它可以防止文件截断。如果您的文件正在被处理,则非常有用logrotate
。
import os
import time
def follow(name):
current = open(name, "r")
curino = os.fstat(current.fileno()).st_ino
while True:
while True:
line = current.readline()
if not line:
break
yield line
try:
if os.stat(name).st_ino != curino:
new = open(name, "r")
current.close()
current = new
curino = os.fstat(current.fileno()).st_ino
continue
except IOError:
pass
time.sleep(1)
if __name__ == '__main__':
fname = "test.log"
for l in follow(fname):
print "LINE: {}".format(l)
解决方案 4:
由于您针对的是 Linux 系统,因此可以使用pyinotify在文件发生更改时通知您。
还有这个技巧,可能对你有用。它用来file.seek
做什么tail -f
。
解决方案 5:
我不是这方面的专家,但我认为您必须使用某种观察者模式来被动地观察文件,然后在发生变化时触发重新打开文件的事件。至于如何实际实现这一点,我不知道。
我不认为 open() 会像您所建议的那样实时打开文件。
解决方案 6:
如果你有在 while 循环中运行的读取文件的代码:
f = open('/tmp/workfile', 'r')
while(1):
line = f.readline()
if line.find("ONE") != -1:
print "Got it"
并且您正在从另一个程序向同一个文件写入数据(附加模式)。只要在文件中附加了“ONE”,您就会得到打印结果。您可以采取任何您想采取的行动。简而言之,您不必定期重新打开文件。
>>> f = open('/tmp/workfile', 'a')
>>> f.write("One
")
>>> f.close()
>>> f = open('/tmp/workfile', 'a')
>>> f.write("ONE
")
>>> f.close()
解决方案 7:
我有一个类似的用例,并为其编写了以下代码片段。虽然有些人可能会认为这不是最理想的做法,但这种方法可以完成工作,而且看起来很容易理解。
def reading_log_files(filename):
with open(filename, "r") as f:
data = f.read().splitlines()
return data
def log_generator(filename, period=1):
data = reading_log_files(filename)
while True:
time.sleep(period)
new_data = reading_log_files(filename)
yield new_data[len(data):]
data = new_data
if __name__ == '__main__':
x = log_generator(</path/to/log/file.log>)
for lines in x:
print(lines)
# lines will be a list of new lines added at the end
希望你觉得这有用
解决方案 8:
这取决于你到底想用这个文件做什么。有两种潜在的用例:
从不断更新的文件(例如日志文件)中读取附加内容。
从不断被覆盖的文件中读取内容(例如 *nix 系统中的网络统计文件)
由于其他人已经详细回答了如何解决场景 #1,我想帮助那些需要场景 #2 的人。基本上,您需要在调用第 n+1次seek(0)
之前使用 (或您想要读取的任何位置)将文件指针重置为 0 。read()
您的代码看起来有点像下面的函数。
def generate_network_statistics(iface='wlan0'):
with open('/sys/class/net/' + iface + '/statistics/' + 'rx' + '_bytes', 'r') as rx:
with open('/sys/class/net/' + iface + '/statistics/' + 'tx' + '_bytes', 'r') as tx:
with open('/proc/uptime', 'r') as uptime:
while True:
receive = int(rx.read())
rx.seek(0)
transmit = int(tx.read())
tx.seek(0)
uptime_seconds = int(uptime.read())
uptime.seek(0)
print("Receive: %i, Transmit: %i" % (receive, transmit))
time.sleep(1)
解决方案 9:
即使在文件末尾返回空字符串,也要保持文件句柄打开,并在休眠一段时间后再次尝试读取它。
import time
syslog = '/var/log/syslog'
sleep_time_in_seconds = 1
try:
with open(syslog, 'r', errors='ignore') as f:
while True:
for line in f:
if line:
print(line.strip())
# do whatever you want to do on the line
time.sleep(sleep_time_in_seconds)
except IOError as e:
print('Cannot open the file {}. Error: {}'.format(syslog, e))