Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to monitor every file creation on a Windows drive?

I'm writing a program in Python that stores information for every file on the system. When it's installed, it will walk over every file, but afterwards I want to update it whenever a new file is created, or when a file is moved. For example, In the Dropbox service, whenever I copy a file into my Dropbox dir, it immediately notices and uploads it to their server.

Is there a way to do this in Python without polling? I'm thinking about some sort of an event listener that is triggered when a file is created.

like image 585
Amir Rachum Avatar asked Dec 06 '25 15:12

Amir Rachum


1 Answers

Try one of:

  • http://pypi.python.org/pypi/watcher
  • http://pypi.python.org/pypi/watchdog

I think first of them is simpler to use. Example of use (from its site):

import watcher
w = watcher.Watcher(dir, callback)
w.flags = watcher.FILE_NOTIFY_CHANGE_FILE_NAME
w.start()

watcher is module that wraps system API, and is described on MSDN:

http://msdn.microsoft.com/en-us/library/aa365465(v=vs.85).aspx

So, flag watcher.FILE_NOTIFY_CHANGE_FILE_NAME says that you will be notified about:

Any file name change in the watched directory or subtree causes a change notification wait operation to return. Changes include renaming, creating, or deleting a file.

like image 133
msztolcman Avatar answered Dec 08 '25 16:12

msztolcman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!