Skip to content

watchdog

watchdog is a Python library and shell utilities to monitor filesystem events.

Cons:

Installation

pip install watchdog

Usage

A simple program that uses watchdog to monitor directories specified as command-line arguments and logs events generated:

import time

from watchdog.events import FileSystemEvent, FileSystemEventHandler
from watchdog.observers import Observer


class MyEventHandler(FileSystemEventHandler):
    def on_any_event(self, event: FileSystemEvent) -> None:
        print(event)


event_handler = MyEventHandler()
observer = Observer()
observer.schedule(event_handler, ".", recursive=True)
observer.start()
try:
    while True:
        time.sleep(1)
finally:
    observer.stop()
    observer.join()

References