- Python Snacks
- Pages
- Sample Issue

👋 Hey there!
Happy Wednesday!
Last week, I emphasized how much you shouldn’t skip over learning the itertools library. Another package to not skip over is watchdog.
Watchdog monitors filesystem for changes - tracking when files are created, modified, moved, or deleted - and triggers events in real time.
Let’s get into it.
Before we get into this week’s snack…
Interested in elevating your Python programming? Python Snacks Pro is the next step forward. With a Python Snacks Pro membership, you’ll get:
A monthly deep dive on a meaningful advanced Python topic - written to teach, not to just tell.
Access to Jupyter notebooks with working code examples in monthly deep dives and periodic weekly articles
Curated resource drops with the best Python links, tools, and ideas from across the web.
And - you’ll help choose what we cover next. Every quarter, pro members vote on future topics so the content stays relevant to your learning.
This isn’t a flood of content to keep up with - it’s a focused, aggregated, and practical way to sharpen your Python skills in just a few hours a month.
If this newsletter has helped you become a better software engineer, this is your way to go deeper and support the work behind it.

📗Article: MkDocs vs Sphinx - Choosing the right project documentation tool.
📰News: Pypi secret token revealed in binary, preventing a supply chain attack.
🎒Tutorial: Understanding the Python Mock Object Library
📕 Article: Monolithic vs Microservice Architecture: Which is right for you?
💻️ Language: Python and SQL are tied for popularity, according to Stack Overflow’s Developer Survey.
📦️ Package: bashplotlib - it’s like matplotlib, but in terminal.

In scenarios where you may want to keep an eye on changes within the file system, you’re going to want to leverage Watchdog.
This library is equipped to allow you to watch for various events such as file modifications, creations, and deletions.
What is Watchdog?
Watchdog will listen for any changes in specified files or directories. When a change occurs, it can trigger custom actions or alerts based upon your code.
For instance, if we want to watch the Documents directory for any deleted files, our code may look as such:
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import time
# Define the behavior on a file deletion
class MyHandler(FileSystemEventHandler):
def on_deleted(self, event):
# Do things when a file is deleted
print(f"{event.src_path} was deleted.")
# Setup the observation for the documents directory
observer = Observer()
event_handler = MyHandler()
observer.schedule(event_handler, path = "/Users/username/Documents", recursive = False)
# Start it
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
We first need to define the behavior of what we should do when a file is deleted. In the above example, we’re simply printing that a file was deleted.
Then, we’re setting up the observer, which does the monitoring in that directory. We’ll start it with observer.start() and keep looping until we kill it (ctrl + c).
Use Cases for Watchdog
The package is powerful for a number of reasons, one of which is how easily it integrates within Python applications.
Here’s a few situations where you may find yourself using the package:
Auto-Updating Applications: For applications that need to reload or update data as soon as a file changes, such as live-updating dashboards or automatic reloads in development environments.
Backup Systems: Automatically backup files when they are changed. This can be used to create versions of files for recovery purposes.
Synchronization Services: Monitor a directory to synchronize its contents with another location, similar to how Dropbox and other file-syncing services work.
Security Monitoring: Detect unauthorized changes in file systems which could indicate a security breach, such as modifications to system files or unexpected access to sensitive data.
Automated Testing: Watch for changes in code files to automatically run tests, helping developers get immediate feedback on their changes.
Installing Watchdog
To install the package, run the following command with pip:
python -m pip install -U watchdog
Similarly with conda:
conda install -c conda-forge watchdog