从经常更新的文件中读取 [英] Reading from a frequently updated file

查看:120
本文介绍了从经常更新的文件中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python系统编写一个程序。目标是读取一个日志文件,并找到一个特定的字符串执行一个bash命令。日志文件正在不断写入另一个程序。我的问题是:

如果我使用open()方法打开文件,我的Python文件对象将被更新为实际文件被其他程序写入或将我必须定时重新打开这个文件?



谢谢

Jim

更新:
感谢迄今为止的答案。我也许应该提到该文件正在被Java EE应用程序写入,所以我无法控制数据写入的时间。我目前有一个程序,每隔10秒重新打开一次文件,并尝试读取它上次读取的文件中的字节位置。目前它只是打印出返回的字符串。我希望这个文件不需要重新打开,但是读命令可以访问Java应用程序写入文件的数据。

 #!/ usr / bin / python 
导入时间
$ b $ fileBytePos = 0
而$ True $ b $ inFile = open('./ server .log','r')
inFile.seek(fileBytePos)
data = inFile.read()
打印数据
fileBytePos = inFile.tell()
打印fileBytePos
inFile.close()
time.sleep(10)

感谢pyinotify和发电机的提示。我会看看这些更好的解决方案。

解决方案

我会建议看着David Beazley的 Python的生成技巧,尤其是第5部分:处理无限数据。它将实时处理Python的等价物 tail -f logfile 命令。

 #follow.py 

#按照tail -f这样的文件。

进口时间
def追踪(thefile):
thefile.seek(0,2)
而真:
line = thefile.readline()
如果不行:
time.sleep(0.1)
继续
产量行

if __name__ =='__main__':
logfile = b

$ b print line,


I'm currently writing a program in python on a Linux system. The objective is to read a log file and execute a bash command upon finding a particular string. The log file is being constantly written to by another program. My question is:

If I open the file using the open() method will my Python file object be updated as the actual file gets written to by the other program or will I have to reopen the file at timed intervals?

Thanks

Jim

UPDATE: Thanks for answers so far. I perhaps should have mentioned that the file is being written to by a Java EE app so I have no control over when data gets written to it. I've currently got a program that reopens the file every 10 seconds and tries to read from the byte position in the file that it last read up to. For the moment it just prints out the string that's returned. I was hoping that the file did not need to be reopened but the read command would somehow have access to the data written to the file by the Java app.

#!/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)

Thanks for the tips on pyinotify and generators. I'm going to have a look at these for a nicer solution.

解决方案

I would recommend looking at David Beazley's Generator Tricks for Python, especially Part 5: Processing Infinite Data. It will handle the Python equivalent of a tail -f logfile command in real-time.

# 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,

这篇关于从经常更新的文件中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆