A watchdog monitors files and directories for changes such as file creation, modification, deletion, or renaming. It is commonly used to automate tasks like processing newly created files, monitoring logs, or triggering actions whenever the contents of a directory change.
Prerequisites
- watchdog: Monitors files and directories for filesystem events.
- logging: Displays information about detected events. This module is included with Python, so no separate installation is required.
Install the watchdog package using:
pip install watchdog
This command installs the watchdog library, which provides tools for monitoring filesystem events in Python.
Monitor Filesystem Changes Using LoggingEventHandler
The LoggingEventHandler class automatically logs filesystem events such as file creation, modification, deletion, and movement. It is useful when you simply want to monitor a directory without writing custom event-handling code.
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
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()
Output
Explanation:
- Creates an Observer to monitor a directory.
- Uses LoggingEventHandler to automatically log all filesystem events.
- Watches the current directory by default.
- Continues monitoring until the program is stopped.
Custom Watchdog Event Handler
Instead of logging every event, you can create a custom event handler to perform specific actions whenever a file is created or modified.
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class OnMyWatch:
watchDirectory = "/give/the/path/of/directory"
def __init__(self):
self.observer = Observer()
def run(self):
handler = Handler()
self.observer.schedule(handler, self.watchDirectory, recursive=True)
self.observer.start()
try:
while True:
time.sleep(5)
except KeyboardInterrupt:
self.observer.stop()
self.observer.join()
class Handler(FileSystemEventHandler):
@staticmethod
def on_any_event(event):
if event.is_directory:
return
if event.event_type == "created":
print("Created:", event.src_path)
elif event.event_type == "modified":
print("Modified:", event.src_path)
if __name__ == "__main__":
watch = OnMyWatch()
watch.run()
Output

Explanation:
- Creates a custom class that inherits from FileSystemEventHandler.
- Detects file creation and modification events.
- Ignores directory events.
- Executes custom code whenever a monitored event occurs.
Watchdog Event Methods
FileSystemEventHandler provides several methods that can be overridden to respond to different filesystem events.
| Method | Description |
|---|---|
| on_any_event() | Runs whenever any filesystem event occurs. |
| on_created() | Executes when a file or directory is created. |
| on_modified() | Executes when a file or directory is modified. |
| on_deleted() | Executes when a file or directory is deleted. |
| on_moved() | Executes when a file or directory is moved or renamed. |
Each event object provides useful information:
- event_type: Type of event (created, modified, deleted, or moved).
- is_directory: Returns True if the event is related to a directory.
- src_path: Path of the affected file or directory.
Monitor Specific File Types
Sometimes you may want to monitor only particular file types instead of every file in a directory. The PatternMatchingEventHandler class allows you to filter events using filename patterns.
import time
import watchdog.events
import watchdog.observers
class Handler(watchdog.events.PatternMatchingEventHandler):
def __init__(self):
super().__init__(
patterns=["*.csv"],
ignore_directories=True,
case_sensitive=False
)
def on_created(self, event):
print("Created:", event.src_path)
def on_modified(self, event):
print("Modified:", event.src_path)
src_path = r"C:\Users\User\Documents"
handler = Handler()
observer = watchdog.observers.Observer()
observer.schedule(handler, path=src_path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Output

Explanation:
- Uses PatternMatchingEventHandler to monitor only .csv files.
- Ignores all other file types.
- Triggers events only when matching files are created or modified.
- You can monitor multiple file types by adding more patterns, such as ["*.csv", "*.txt", "*.json"].