Watchdog

6.0.0 · active · verified Sat Mar 28

Watchdog is a Python library and shell utility that provides an API for monitoring file system events in real-time. It supports various operating systems by utilizing native APIs like inotify (Linux), FSEvents (macOS), and ReadDirectoryChangesW (Windows), falling back to polling when native APIs are unavailable. Currently at version 6.0.0, the library maintains an active development pace with major releases approximately annually.

Warnings

Install

Imports

Quickstart

This quickstart monitors the current directory ('.') recursively for any filesystem events using a custom event handler and prints them to the console. The observer runs in a separate thread until a KeyboardInterrupt is received.

import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class MyEventHandler(FileSystemEventHandler):
    def on_any_event(self, event):
        # Log any event for demonstration
        print(f"Event type: {event.event_type} | Path: {event.src_path} | Is directory: {event.is_directory}")

if __name__ == "__main__":
    path_to_watch = "."
    event_handler = MyEventHandler()
    observer = Observer()
    observer.schedule(event_handler, path_to_watch, recursive=True)
    observer.start()
    try:
        print(f"Monitoring directory: {path_to_watch}")
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

view raw JSON →