Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run an function when anything changes in a dir with Python Watchdog?

I'm trying to use watchdog to run a sync script whenever anything changes in a dir (except for one specific file). I simply copied the code from the readme (pasted below), which does what it says; log which file has changed.

import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    path = sys.argv[1] if len(sys.argv) > 1 else '.'
    event_handler = LoggingEventHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

I now want to run a function (which syncs the whole folder to a remote machine) whenever anything changes. So I just replaced event_handler with my own function. But that gives me the following error:

Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/Library/Python/2.7/site-packages/watchdog/observers/api.py", line 199, in run
    self.dispatch_events(self.event_queue, self.timeout)
  File "/Library/Python/2.7/site-packages/watchdog/observers/api.py", line 368, in dispatch_events
    handler.dispatch(event)
AttributeError: 'function' object has no attribute 'dispatch'

Does anybody know what I'm doing wrong here? All tips are welcome!

ps. I also want to exclude one file in the folder from being watched. Any ideas how I should do that?

like image 548
kramer65 Avatar asked Oct 03 '15 14:10

kramer65


People also ask

How do I monitor a directory change in python?

To use this, just run python watcher.py from your console. If you open any console window and upload a file or make any changes, you will see this printed out in your original console window. To actually make this useful, you should wrap this up as a daemon or upstart script which can be run indefinitely.

How does watchdog work python?

watchdog is an open-source python API library that is a cross-platform API to monitor file system events. You can specify a folder or a directory to watchdog observer, which keeps monitoring the folder for any changes like file creation, modification, deletion, or moving of files from one folder to another.


1 Answers

You need to subclass and do whatever you want in dispatch:

import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler

class Event(LoggingEventHandler):
    def dispatch(self, event):
        print("Foobar")

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    path = sys.argv[1] if len(sys.argv) > 1 else '.'
    event_handler = Event()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

If you run the code you will see Foobar outputted whenever a change is detected, to ignore files you may need to use [events.PatternMatchingEventHandler][1]. There are various methods in each

To do something when a something is modified case we can override on_modified:

class Event(LoggingEventHandler):
    def on_modified(self, event):
        print("Doh")

And running the code using the class above with event_handler = Event() and changing a file will output something like:

Doh
Doh
Doh
Doh
Doh
Doh
Doh
2015-10-03 15:33:55 - Created file: ./test.txt___jb_bak___
2015-10-03 15:33:55 - Moved file: from ./test.txt to ./test.txt___jb_old___
2015-10-03 15:33:55 - Moved file: from ./test.txt___jb_bak___ to ./test.txt
2015-10-03 15:33:55 - Deleted file: ./test.txt___jb_old___
Doh

[1]: http://pythonhosted.org/watchdog/api.html#watchdog.events.PatternMatchingEventHandler EventHandler class you can override, it all depends on what it is you want to do. The LoggingEventHandler class itslef is a subclass of watchdog.events.FileSystemEventHandler:

class watchdog.events.FileSystemEventHandler Bases: object

Base file system event handler that you can override methods from.

dispatch(event) Dispatches events to the appropriate methods.

Parameters: event (FileSystemEvent) – The event object representing the file system event.

on_any_event(event) Catch-all event handler.

Parameters: event (FileSystemEvent) – The event object representing the file system event.

on_created(event) Called when a file or directory is created.

Parameters: event (DirCreatedEvent or FileCreatedEvent) – Event representing file/directory creation.

on_deleted(event) Called when a file or directory is deleted.

Parameters: event (DirDeletedEvent or FileDeletedEvent) – Event representing file/directory deletion.

on_modified(event) Called when a file or directory is modified.

Parameters: event (DirModifiedEvent or FileModifiedEvent) – Event representing file/directory modification.

on_moved(event) Called when a file or a directory is moved or renamed.

Parameters: event (DirMovedEvent or FileMovedEvent) – Event representing file/directory movement.
like image 117
Padraic Cunningham Avatar answered Sep 24 '22 08:09

Padraic Cunningham