3

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.

Amir Rachum
  • 76,817
  • 74
  • 166
  • 248
  • possible duplicate of [Monitoring files/directories with python](http://stackoverflow.com/questions/597903/monitoring-files-directories-with-python) – Piotr Dobrogost May 07 '12 at 09:54

1 Answers1

2

Try one of:

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.

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
msztolcman
  • 397
  • 3
  • 10
  • 2
    Your answer would be considerably better if you could summarise the information in the links and explain why you think the OP should try one of your suggestions. – Ben May 06 '12 at 21:15
  • 1
    I would consider watchdog to be vastly superior to watcher because it is cross-platform. While only Windows support may be needed at present, the reduction of a Windows dependency may save a vast amount of effort later. – Chris Morgan May 07 '12 at 10:34