使用 pyinotify 监视文件创建,但等待它完全写入磁盘

发布于 2024-09-26 13:13:00 字数 180 浏览 4 评论 0原文

我正在使用 pyinotify 来监视文件夹中何时创建文件。当创建某些文件时,我想移动它们。问题是,一旦创建文件(显然),我的程序就会尝试移动它,甚至在它完全写入磁盘之前。

有没有办法让 pyinotify 等到文件完全写入磁盘后再通知我它已创建?或者有什么简单的方法可以在我收到通知后让 python 等待移动它直到它完成编写?

I'm using pyinotify to watch a folder for when files are created in it. And when certain files are created I want to move them. The problem is that as soon as the file is created (obviously), my program tries to move it, even before it's completely written to disk.

Is there a way to make pyinotify wait until a file is completely written to disk before notifying me that it's been created? Or is there any easy way to, after I'm notified, make python wait to move it until it's done being written?

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

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

发布评论

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

评论(3

﹏雨一样淡蓝的深情 2024-10-03 13:13:00

让 pyinotify 对 IN_CLOSE_WRITE 事件做出反应:

wm.add_watch(watched_dir, pyinotify.IN_CLOSE_WRITE, proc_fun=MyProcessEvent())

这是来自 man 5 incrontab,但它同样适用于 pyinotify:

   IN_ACCESS           File was accessed (read) (*)
   IN_ATTRIB           Metadata changed (permissions, timestamps, extended attributes, etc.) (*)
   IN_CLOSE_WRITE      File opened for writing was closed (*)
   IN_CLOSE_NOWRITE    File not opened for writing was closed (*)
   IN_CREATE           File/directory created in watched directory (*)
   IN_DELETE           File/directory deleted from watched directory (*)
   IN_DELETE_SELF           Watched file/directory was itself deleted
   IN_MODIFY           File was modified (*)
   IN_MOVE_SELF        Watched file/directory was itself moved
   IN_MOVED_FROM       File moved out of watched directory (*)
   IN_MOVED_TO         File moved into watched directory (*)
   IN_OPEN             File was opened (*)

Have pyinotify react to IN_CLOSE_WRITE events:

wm.add_watch(watched_dir, pyinotify.IN_CLOSE_WRITE, proc_fun=MyProcessEvent())

This is from man 5 incrontab, but it applies equally well to pyinotify:

   IN_ACCESS           File was accessed (read) (*)
   IN_ATTRIB           Metadata changed (permissions, timestamps, extended attributes, etc.) (*)
   IN_CLOSE_WRITE      File opened for writing was closed (*)
   IN_CLOSE_NOWRITE    File not opened for writing was closed (*)
   IN_CREATE           File/directory created in watched directory (*)
   IN_DELETE           File/directory deleted from watched directory (*)
   IN_DELETE_SELF           Watched file/directory was itself deleted
   IN_MODIFY           File was modified (*)
   IN_MOVE_SELF        Watched file/directory was itself moved
   IN_MOVED_FROM       File moved out of watched directory (*)
   IN_MOVED_TO         File moved into watched directory (*)
   IN_OPEN             File was opened (*)
夜灵血窟げ 2024-10-03 13:13:00

在这个级别很难判断文件是否正在被写入。您可以做的就是测试文件是否被其他进程打开。

1) 从打开文件时使用的各种标志来看,O_EXLOCK 标志可能会有所帮助。
如果设置了 O_EXLOCK 标志,则文件描述符对该文件具有独占锁。
所以我的理解是,如果您可以使用 O_EXLOCK 标志执行 os.open() ,则它不会被其他进程打开。
这应该适用于所有 posix 兼容操作系统,但我还没有测试过。如果文件已打开,那么您可以关闭,等待并重试。

2)您还可以尝试 os.stat 并查看更改的时间戳并尝试安全地解释信息。尽管这并不是万无一失的。

3)
在 unix 系统上,您可以尝试“lsof”

4) 以下页面描述了如何使用 /proc/PID/fd 中的符号链接来测试打开的文件

[编辑:链接已更新]

It is quite difficult to tell at this level if a file is being written to. What you can do is test to see if a file is opened by some other process.

1) From the various flags that are used while opening a file, O_EXLOCK flag might be of help.
If the O_EXLOCK flag is set, the file descriptor has an exclusive lock on the file.
So my understanding is if you can do os.open() with O_EXLOCK flag, it's not open by other process.
This should work on all posix compatible OS but I have not tested it. If the file, is open then you could close, wait and retry again.

2) You can also try os.stat and see changing time stamp and try to safely interpret the information. Though this is not fool proof.

3)
On unix systems, you can try "lsof"

4) The following page describes use of symlinks from /proc/PID/fd to test for open files

[Edit : Links updated]

初雪 2024-10-03 13:13:00

如果您可以控制写入过程,则可以在写入文件时调用文件“foo.part”,并在关闭文件时将其重命名为“foo”。

If you have control of the writing process, you could call the file "foo.part" while it is being written to and rename it to "foo" when it has been closed.

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