Everyday Utilities

watchdog

Software that never blinks, watching your files so something else can react the instant they change.

Install it: pip install watchdog

What does it do?

Watchdog keeps an eye on folders and files on your computer and instantly notifies a program the moment something changes, a file gets created, edited, or deleted, without that program constantly checking over and over. It works across Windows, Mac, and Linux, tapping into each operating system’s own efficient way of tracking file changes rather than wastefully re-scanning folders on a timer. It’s the mechanism behind a website automatically refreshing the instant you save a code change, or a sync app noticing a new file within milliseconds. Developers usually set it up to run an action, like restarting a server or re-running a test, whenever it detects a relevant change.

See it in action

This watches a folder on your computer and instantly prints a message the moment any file inside it is changed.

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

class Handler(FileSystemEventHandler):
    def on_modified(self, event):
        print(f"Changed: {event.src_path}")

observer = Observer()
observer.schedule(Handler(), path="path/to/watch", recursive=True)
observer.start()

Why would a non-developer care?

The instant, live-reloading feel of a lot of modern development tools, where you save a file and immediately see the result update, depends on exactly this kind of file watching. Beyond development, it’s the same basic idea behind sync services and backup tools that notice new files right away instead of on a slow schedule.

Real-world examples

Many popular Python development tools use file watching similar to what watchdog provides to power auto-reload features, letting a web server or docs site rebuild the moment you save a change. It’s also used in automation scripts that watch a folder for new invoices dropped in by a scanner and immediately process each one.

Who uses it

Developers building auto-reloading tools and automation scripts that need to react immediately to file changes rather than checking on a fixed schedule.

How it compares to alternatives

It replaces the crude alternative of writing a loop that repeatedly checks a folder every few seconds, and compared to operating-system-specific tools, its main advantage is one consistent interface that works the same way across Windows, macOS, and Linux.

New to Python and want to actually try libraries like this yourself?

Find a beginner-friendly course

Related libraries