Using pyinotify to watch for file creation, but waiting for it to be completely written to disk

隐身守侯 提交于 2019-11-29 19:19:59

问题


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?


回答1:


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



回答2:


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

  • http://www.gossamer-threads.com/lists/python/python/639874

[Edit : Links updated]

  • https://launchpad.net/ubuntu/%2Bsource/lsof



回答3:


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.



来源:https://stackoverflow.com/questions/3876348/using-pyinotify-to-watch-for-file-creation-but-waiting-for-it-to-be-completely

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!