• Methods to watch a directory (and files) for changes
    7 replies, posted
Hi, I'm writing a DropBox clone for experience, and I'd like to know a bit more about methods to watch directories for changes. By reading some pages found via Google, I found out that walking the directory tree every [n] seconds and looking for changes on all files can become very hard to do when the folder contains many files. I've also read about some events Windows dispatches when the folder is changed, which require way less processing power but aren't fit for other OSes. I'm basically looking for a method which would work on all main OSes (mac, linux, windows, unix) which would have the best performance.
[url=http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx]System.IO.FileSystemWatcher[/url] seems to do exactly the job.
[QUOTE=ZeekyHBomb;27892768][url=http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx]System.IO.FileSystemWatcher[/url] seems to do exactly the job.[/QUOTE] That would not work very well as a cross-OS solution though...
Mono fully implements System.IO iirc.
[QUOTE=ZeekyHBomb;27893349]Mono fully implements System.IO iirc.[/QUOTE] Yeah but firstly, I'd like this to run natively, not with any help from other resources like Mono. Secondly, I'd like to do this in python, and this solution looks like it would only work on .NET.
[QUOTE=TerabyteS;27893433]Yeah but firstly, I'd like this to run natively, not with any help from other resources like Mono. Secondly, I'd like to do this in python, and this solution looks like it would only work on .NET.[/QUOTE] I think the functionality you're looking for may not be implemented in any standard Python library. On Win32, you can glue together [url=http://msdn.microsoft.com/en-us/library/aa365261(v=vs.85).aspx]some WinAPI calls[/url] using ctypes. I don't know what about other system though. You can always fall back to the manual scanning for changes.
[QUOTE=q3k;27893488]I think the functionality you're looking for may not be implemented in any standard Python library. On Win32, you can glue together [url=http://msdn.microsoft.com/en-us/library/aa365261(v=vs.85).aspx]some WinAPI calls[/url] using ctypes. I don't know what about other system though. You can always fall back to the manual scanning for changes.[/QUOTE] I've had a try at manual scanning and it's really slow (I walk the entire directory tree for all files and get their last modified time and then check it with every file's old update time). Is there a better method?
To do it with Windows you can use [url=http://sourceforge.net/projects/pywin32/]pywin32[/url] and [url=http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html#use_readdirectorychanges]this[/url], To do it on Linux use [url=https://github.com/seb-m/pyinotify]pyinotify[/url].
Sorry, you need to Log In to post a reply to this thread.