问题描述
我正在使用pyinotify监视文件夹中创建文件的时间.当创建某些文件时,我想移动它们.问题是,一旦创建了文件(显然),我的程序就会尝试将其移动,甚至在将其完全写入磁盘之前也是如此.
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.
是否有办法让pyinotify等待文件完全写入磁盘,然后通知我已创建文件?还是有什么简单的方法可以让我在接到通知后让python等待移动它直到完成编写?
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?
推荐答案
让pyinotify对 IN_CLOSE_WRITE 事件:
Have pyinotify react to IN_CLOSE_WRITE events:
wm.add_watch(watched_dir, pyinotify.IN_CLOSE_WRITE, proc_fun=MyProcessEvent())
这来自man 5 incrontab
,但对pyinotify也同样适用:
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 (*)
这篇关于使用pyinotify监视文件创建,但等待将其完全写入磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!