使用 python 检测新的或修改的文件
我试图检测何时在目录中创建新文件或何时在目录中修改现有文件。
我尝试寻找一个可以执行此操作的脚本(最好是在 python 或 bash 中),但没有找到。我的环境是使用 Python 2.6 的 Linux
I'm trying to detect when a new file is created a directory or when an existing file is modified in a directory.
I tried searching for a script that would do this (preferably in python or bash) but came up short. My environment is linux with Python 2.6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 gio,它是 GLib 的文件系统部分(在 GLib 的 python 绑定中),
但是,您的程序必须运行 GLib 主循环才能使事件到达。一种快速测试方法是使用:
GLib 是一个非常适合应用程序的高级库。您不必关心它使用哪个底层系统来进行文件监控。
我现在看到您使用 Fedora Core 2。真的是版本 2 吗?这可能太旧了,无法在 GLib 中使用 GIO。前面提到的 Pyinotify 可能是一个更好的解决方案,尽管可移植性较差。
You can use
gio
which is the Filesystem part of GLib (In GLib's python bindings)however, your program must be running a GLib mainloop for the events to arrive. One quick way to test that is by using:
GLib is a high-level library which is well suited for Applications. You don't have to care about which underlying system it uses for the file monitoring.
I now see you use Fedora Core 2. Really version 2? That might be too old to use GIO in GLib. Pyinotify that has been mentioned might be a better solution, although less portable.
如果您使用的是 Linux,您可以尝试
pyinotify
,它的作用类似于inotify(7)
系统的面向对象包装器来电。该项目网站包含非常简单的教程和示例。If you're using Linux, you may try
pyinotify
that acts like an object-oriented wrapper around theinotify(7)
system calls. The project website contains quite straightforward tutorials and examples.如果您可以使用 PyQt,那么 QFileSystemWatcher 就可以做到这一点。
If you can use PyQt, there's QFileSystemWatcher that does just this.
还有gamin 的 Python 绑定。
There are also Python bindings for gamin.