Monitor Your File System With Python’s Watchdog

Date:

Share post:


Picture by Writer | DALLE-3 & Canva

 

Python’s watchdog library makes it simple to observe your file system and reply to those modifications robotically. Watchdog is a cross-platform API that lets you run instructions in response to any modifications within the file system being monitored. We are able to set triggers on a number of occasions equivalent to file creation, modification, deletion, and motion, after which reply to those modifications with our customized scripts.

 

Setup for Watchdog

 

You may want two modules to start:

  • Watchdog: Run this command beneath within the terminal to put in the watchdog.

     

  • Logging: It’s a built-in Python module, so there isn’t any have to externally set up it.

 

Fundamental Utilization

 

Let’s create a easy script ‘main.py’ that displays a listing and prints a message each time a file is created, modified, or deleted.

 

Step 1: Import Required Modules

First, import the required modules from the watchdog library:

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

 

Step 2: Outline Occasion Handler Class

We outline a category MyHandler that inherits from FileSystemEventHandler. This class overrides strategies like on_modified, on_created, and on_deleted to specify what to do when these occasions happen. The occasion handler object might be notified when any modifications occur within the file system.

class MyHandler(FileSystemEventHandler):
    def on_modified(self, occasion):
        print(f'File {occasion.src_path} has been modified')

    def on_created(self, occasion):
        print(f'File {occasion.src_path} has been created')

    def on_deleted(self, occasion):
        print(f'File {occasion.src_path} has been deleted')

 

Some helpful strategies of FileSystemEventHandler are defined beneath.

  • on_any_event: Executed for any occasion.
  • on_created: Executed upon creation of a brand new file or listing.
  • on_modified: Executed upon modification of a file or when a listing is renamed.
  • on_deleted: Triggered upon the deletion of a file or listing.
  • on_moved: Triggered when a file or listing is relocated.

 

Step 3: Initialize and Run the Observer

The Observer class is answerable for monitoring the file system for any modifications and subsequently notifying the occasion handler. It repeatedly tracks file system actions to detect any updates.

if __name__ == "__main__":
    event_handler = MyHandler()
    observer = Observer()
    observer.schedule(event_handler, path=".", recursive=True)
    observer.begin()

    attempt:
        whereas True:
            time.sleep(1)
    besides KeyboardInterrupt:
        observer.cease()
    observer.be part of()

 

We begin the observer and use a loop to maintain it operating. Once you wish to cease it, you’ll be able to interrupt with a keyboard sign (Ctrl+C).

 

Step 4: Run the Script

Lastly, run the script with the next command.

 

Output:

File .File1.txt has been modified
File .New Textual content Doc (2).txt has been created
File .New Textual content Doc (2).txt has been deleted
File .New Textual content Doc.txt has been deleted

 

The above code will log all of the modifications within the listing to the terminal if any file/folder is created, modified, or deleted.

 

Superior Utilization

 

Within the following instance, we are going to discover the right way to arrange a system that detects any change in Python information and run exams for it robotically. We have to set up pytest with the next command.

 

Step 1: Create a Easy Python Undertaking With Exams

First, arrange the fundamental construction of your undertaking:

my_project/
│
├── src/
│   ├── __init__.py
│   └── instance.py
│
├── exams/
│   ├── __init__.py
│   └── test_example.py
│
└── watchdog_test_runner.py

 

Step 2: Write Code in Instance Python File

Create a easy Python module in src/instance.py:

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

 

Step 3: Write the Check Circumstances

Subsequent, write the take a look at instances for features in exams/test_example.py:

import pytest
from src.instance import add, subtract

def test_add():
    assert add(1, 2) == 3
    assert add(-1, 1) == 0
    assert add(-1, -1) == -2

def test_subtract():
    assert subtract(2, 1) == 1
    assert subtract(1, 1) == 0
    assert subtract(1, -1) == 2

 

Step 4: Write the Watchdog Script

Now, create the watchdog_test_runner.py script to observe modifications in Python information and robotically run exams:

import time
import subprocess
from watchdog.observers import Observer
from watchdog.occasions import FileSystemEventHandler

class TestRunnerHandler(FileSystemEventHandler):
    def on_modified(self, occasion):
        if occasion.src_path.endswith('.py'):
            self.run_tests()

    def run_tests(self):
        attempt:
            end result = subprocess.run(['pytest'], examine=False, capture_output=True, textual content=True)
            print(end result.stdout)
            print(end result.stderr)
            if end result.returncode == 0:
                print("Tests passed successfully.")
            else:
                print("Some tests failed.")
        besides subprocess.CalledProcessError as e:
            print(f"Error running tests: {e}")

if __name__ == "__main__":
    path = "."  # Listing to look at
    event_handler = TestRunnerHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    
    observer.begin()
    print(f"Watching for changes in {path}...")

    attempt:
        whereas True:
            time.sleep(1)
    besides KeyboardInterrupt:
        observer.cease()
    
    observer.be part of()

 

Step 5: Run the Watchdog Script

Ultimately, open a terminal, navigate to your undertaking listing (my_project), and run the watchdog script:

python watchdog_test_runner.py

 

Output:

Looking forward to modifications in ....
========================= take a look at session begins =============================
platform win32 -- Python 3.9.13, pytest-8.2.1, pluggy-1.5.0
rootdir: F:Net Devwatchdog
plugins: anyio-3.7.1
collected 2 gadgets

teststest_example.py ..                                                 [100%]

========================== 2 handed in 0.04s ==============================

Exams handed efficiently.

 

This output reveals that every one the take a look at instances are handed after modifications had been made to instance.py file.

 

Summing Up

 

Python’s watchdog library is a robust software for monitoring your file system. Whether or not you are automating duties, syncing information, or constructing extra responsive functions, watchdog makes it simple to react to file system modifications in actual time. With just some traces of code, you can begin monitoring directories and dealing with occasions to streamline your workflow.

 
 

Kanwal Mehreen Kanwal is a machine studying engineer and a technical author with a profound ardour for knowledge science and the intersection of AI with medication. She co-authored the book “Maximizing Productivity with ChatGPT”. As a Google Technology Scholar 2022 for APAC, she champions range and educational excellence. She’s additionally acknowledged as a Teradata Variety in Tech Scholar, Mitacs Globalink Analysis Scholar, and Harvard WeCode Scholar. Kanwal is an ardent advocate for change, having based FEMCodes to empower ladies in STEM fields.

Related articles

Actual Identities Can Be Recovered From Artificial Datasets

If 2022 marked the second when generative AI’s disruptive potential first captured vast public consideration, 2024 has been...

Shaping the Way forward for Leisure

Disney has at all times been on the forefront of innovation. From groundbreaking animated movies like Snow White...

Huawei’s Ascend 910C: A Daring Problem to NVIDIA within the AI Chip Market

The Synthetic Intelligence (AI) chip market has been rising quickly, pushed by elevated demand for processors that may...

Heico Sandee, Founder and CEO of Good Robotics – Interview Sequence

Heico Sandee, is the Co-Founder and CEO of Good Robotics.Good Robotics provides know-how and companies designed to automate...