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

发布于 2021-01-29 15:56:48

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

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

关注者
0
被浏览
148
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

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


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看