简体   繁体   中英

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

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?

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

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

[Edit : Links updated]

如果您可以控制写入过程,则可以在写入文件时调用文件“foo.part”,并在关闭时将其重命名为“foo”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM