小编典典

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

python

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

有没有办法让pyinotify等待文件完全写入磁盘,然后通知我已创建文件?还是有任何简单的方法可以让python通知我,直到它被写完后才将其移动?


阅读 215

收藏
2021-01-20

共1个答案

小编典典

让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 (*)
2021-01-20