How to run a function when something changes in a directory using Python Watchdog?

I am trying to use watchdog to start the synchronization script whenever something changes in the directory (except for one specific file). I just copied the code from readme (pasted below), which does what it says; The log whose 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()

Now I want to run a function (which synchronizes the entire folder with the remote machine) whenever something changes. So I just replaced it with event_handlermy own function. But this 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 anyone know what I'm doing wrong here? All tips are welcome!

ps. I also want one file in a folder not to be viewed. Any ideas how I should do this?

+4
1

, :

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()

, Foobar, , , , [events.PatternMatchingEventHandler] [1].

- , - , on_modified:

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

event_handler = Event() - :

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 , , , . LoggingEventHandler itslef watchdog.events.FileSystemEventHandler:

watchdog.events.FileSystemEventHandler  :

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

()   .

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

on_any_event ()   Catch-all.

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

on_created ()  , .

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

on_deleted ()  , .

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

on_modified ()   .

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

on_moved ()  , .

Parameters: event (DirMovedEvent or FileMovedEvent) – Event representing file/directory movement.
+10

Source: https://habr.com/ru/post/1610095/


All Articles