👋 Hey there!

Each newsletter I try and vary the content up a bit, but sometimes I’ll stick with the same format (spoiler alert)

Last week, I emphasized how much you shouldn’t skip over learning the itertools library. This week, I’m bringing another package to your inbox: watchdog.

Let’s get into it.

Final note: I get it, ads can be annoying. If you want an ad-free experience and want to support the newsletter, upgrade your account today to go ad-free.

Before we get into this week’s snack…

Have a cool, product, job posting, or something you want to get in front of a large technical audience geared towards Python programmers? It just so happens that we’re looking for advertisers!

With your ad, you’ll get in front of 300+ subscribers with a >50% open rate and an average of 6% CTR. With your ad, you’ll get:

  • Up to 300 characters

  • 1 image

  • 2 CTA’s

📗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

📦️ 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:

  1. 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.

  2. Backup Systems: Automatically backup files when they are changed. This can be used to create versions of files for recovery purposes.

  3. Synchronization Services: Monitor a directory to synchronize its contents with another location, similar to how Dropbox and other file-syncing services work.

  4. 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.

  5. 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