Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to track changes on files like dropbox does?

Tags:

python

dropbox

Anybody know how to execute methods (python) when files are modified like Dropbox and his Continuous Data Protection mechanism that can track exactly when a file is modified and sync it.

Of course it would not be of the entire hard-disk, but a track on a specified directory.

OBS: For Windows and Linux OS. Mac is a plus ;)

like image 800
Pabluez Avatar asked Jan 19 '23 02:01

Pabluez


2 Answers

On Linux, pyinotify will probably do what you want. But note the caveats mentioned in the inotify(7) manpage, in particular:

Note that the event queue can overflow. In this case, events are lost. Robust applications should handle the possibility of lost events gracefully.

If monitoring an entire directory subtree, and a new subdirectory is created in that tree, be aware that by the time you create a watch for the new subdirectory, new files may already have been created in the subdirectory. Therefore, you may want to scan the contents of the subdirectory immediately after adding the watch.

like image 192
Thomas Avatar answered Jan 28 '23 16:01

Thomas


I'm not sure if Python has any cross-platform solution for this, but if you are only interested in Windows-based solutions, you should look into directory change notifications. To call the Win32 API functions, you can look into pywin32.

On Linux, there seems to be a bunch of solutions, including fschange, dnotify and inotify. I'm not sure which one is the recommended solution, but inotify seems to be the most complete solution.

Not all platforms have such a feature. If it's not available for a given platform, you'll have to emulate such notifications by checking directory contents periodically.

like image 35
André Caron Avatar answered Jan 28 '23 16:01

André Caron