views:

92

answers:

2

In Linux, How can we know if a file has completed copying before reading it?.
(In Windows, OSError is raised.)

+1  A: 

In Linux, you can open a file while another process is writing to it without Python throwing an OSError, so in general, you cannot know for sure whether the other side has finished writing into that file. You can try some hacks, though:

  1. You can check the file size regularly to see whether it increased since the last check. If it hasn't increased in, say, five seconds, you might be safe to assume that the copy has finished. I'm saying might since this is not true in all circumstances. If the other process that is writing the file is blocked for whatever reason, it might temporarily stop writing to the file and resume it later. So this is not 100% fool-proof, but might work for local file copies if the system is never under a heavy load that would stall the writing process.

  2. You can check the output of fuser (this is a shell command), which will list the process IDs for all the files that are holding a file handle to a given file name. If this list includes any process other than yours, you can assume that the copying process hasn't finished yet. However, you will have to make sure that fuser is installed on the target system in order to make it work.

Tamás
I was using the polling method. I wanted to know if there is a better way....
asdfg
+1  A: 

You can use the inotify mechanisms (via pyinotify) to catch events like CREATE, WRITE, CLOSE and based on them you can assume wether the copy has finished or not.

However, since you provided no details on what are you trying to do, I can't tell if inotify would be suitable for you (btw, inotify is Linux specific so you can't use it on Windows or other platforms)

Unknown
Note that the homepage of pyinotify is now http://trac.dbzteam.org/pyinotify instead of the SourceForge page. Anyway, great answer, I didn't know about this module!
Tamás
It works... Thanks.
asdfg