Dectecting dir changes
fredrik at pythonware.com
Fri Dec 9 16:40:12 CET 2005
"chuck" <cmedcoff at gmail.com> wrote:
> I need to write a daemon for Solaris that monitors a directory for
> incoming FTP transfers. Under certain conditions, when the transfer is
> complete I need to send an email notification, and do other stuff.
> Win32 provides FindFirstChangeNotification(), but as best I can tell
> this isn't supported on Solaris.
> I am thinking of using the approach suggested here
> which is:
> import os, time
> path_to_watch = "."
> before = dict ([(f, None) for f in os.listdir (path_to_watch)])
> while 1:
> time.sleep (10)
> after = dict ([(f, None) for f in os.listdir (path_to_watch)])
> added = [f for f in after if not f in before]
> removed = [f for f in before if not f in after]
> if added: print "Added: ", ", ".join (added)
> if removed: print "Removed: ", ", ".join (removed)
> before = after
> My concern with this is that a change may be detected before the ftp
> daemon process is done writing the file to disk. I don't want to take
> any action until the file is written and closed. I know that I could
> pole a new file looping to check to see if it's file size is changing
> but the timing of such a loop is subject to I/O buffering and is
> otherwise not elegant.
there is no reliable way to do this, unless you can modify the sending
application to use a temporary filename during transfer (or find an FTP
server that does this automatically), or you're willing to write your own
checking the mtime for new files might be good enough for some use
cases (poll the directory at regular intervals; if a file is newer than X
seconds, assume it's still being updated).
More information about the Python-list