在 Python 中动态读取更新的文件

发布于 2024-10-28 05:15:07 字数 248 浏览 1 评论 0原文

我正在编写两个都解析文件的 Python 脚本。一个是标准的 unix 日志文件,另一个是二进制文件。我正在尝试找出监控这些数据的最佳方法,以便我可以在数据更新后立即读取数据。到目前为止,我发现的大多数解决方案都是特定于 Linux 的,但我需要它在 FreeBSD 中工作。

显然,一种方法是每隔 X 时间运行我的脚本,但这看起来很粗鲁且效率低下。如果我希望我的 Python 应用程序在后台持续运行,监视文件并在文件更改/更新后对其进行操作,我最好的选择是什么?

I'm writing two Python scripts that both parse files. One is a standard unix logfile and the other is a binary file. I'm trying to figure out the best way to monitor these so I can read data as soon as they're updated. Most of the solutions I've found thus far are linux specific, but I need this to work in FreeBSD.

Obviously one approach would be to just run my script every X amount of time, but this seems gross and inefficient. If I want my Python app running continuously in the background monitoring a file and acting on it once it's changed/updated, what's my best bet?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩心态 2024-11-04 05:15:08

您尝试过 KQueue 事件吗?

http://docs.python.org/library/select.html#kqueue-objects

kqueue 是 FreeBSD/OS 版本的 inotify(文件更改通知服务)。我没用过这个,但我想这就是你想要的。

Have you tried KQueue events?

http://docs.python.org/library/select.html#kqueue-objects

kqueue is the FreeBSD / OS version of inotify (file change notification service). I haven't used this, but I think it's what you want.

酸甜透明夹心 2024-11-04 05:15:08

我曾经为用 Python 构建的解析器创建了一种守护进程。我需要观看一系列文件并用 Python 处理它们,它必须是一个真正的多操作系统解决方案(在本例中为 Windows 和 Linux)。我编写了一个程序,通过检查文件的修改时间来监视文件列表。程序休眠一段时间,然后检查正在观看的文件的修改时间。如果修改时间比之前注册的时间新,则文件已更改,因此必须对此文件进行处理。

类似这样的:

import os
import time

path = os.path.dirname(__file__)
print "Looking for files in", path, "..."

# get interesting files
files = [{"file" : f} for f in os.listdir(path) if os.path.isfile(f) and os.path.splitext(f)[1].lower() == ".src"]
for f in files:
    f["output"] = os.path.splitext(f["file"])[0] + ".out"
    f["modtime"] = os.path.getmtime(f["file"]) - 10
    print "  watching:", f["file"]


while True:
    # sleep for a while
    time.sleep(0.5)
    # check if anything changed
    for f in files:
        # is mod time of file is newer than the one registered?
        if os.path.getmtime(f["file"]) > f["modtime"]: 
            # store new time and...
            f["modtime"] = os.path.getmtime(f["file"])
            print f["file"], "has changed..."
            # do your stuff here

它看起来不像顶级代码,但它运行得很好。

还有其他与此相关的问题,但我不知道他们是否会直接回答您的问题:

如何实现 tail -F 的 pythonic 等效项?

如何监视文件的更改?

我如何“监视”文件的修改/更改?

希望这会有所帮助!

I once did to make a sort of daemon process for a parser built in Python. I needed to watch a series of files and process them in Python, and it had to be a truly multi-OS solution (Windows & Linux in this case). I wrote a program that watches over a list of files by checking their modification time. The program sleeps for a while and then checks the modification time of the files being watched. If the modification time is newer than the one previously registered, then the file has changed and so stuff has to be done with this file.

Something like this:

import os
import time

path = os.path.dirname(__file__)
print "Looking for files in", path, "..."

# get interesting files
files = [{"file" : f} for f in os.listdir(path) if os.path.isfile(f) and os.path.splitext(f)[1].lower() == ".src"]
for f in files:
    f["output"] = os.path.splitext(f["file"])[0] + ".out"
    f["modtime"] = os.path.getmtime(f["file"]) - 10
    print "  watching:", f["file"]


while True:
    # sleep for a while
    time.sleep(0.5)
    # check if anything changed
    for f in files:
        # is mod time of file is newer than the one registered?
        if os.path.getmtime(f["file"]) > f["modtime"]: 
            # store new time and...
            f["modtime"] = os.path.getmtime(f["file"])
            print f["file"], "has changed..."
            # do your stuff here

It does not look like top notch code, but it works quite well.

There are other SO questions about this, but I don't know if they'll provide a direct answer to your question:

How to implement a pythonic equivalent of tail -F?

How do I watch a file for changes?

How can I "watch" a file for modification / change?

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文