gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:23:50 25 4
gpt4 key购买 nike

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

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

最佳答案

让 pyinotify 对 IN_CLOSE_WRITE 使用react事件:

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 (*)

关于python - 使用 pyinotify 监视文件创建,但等待它完全写入磁盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3876348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com