
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Monitor Python Files for Changes
Monitoring Python files for changes is essential in many development and automation scenarios such as triggering automatic reloads, running tests, or updating services when the source code is modified. This is particularly useful in web development, machine learning pipelines or custom tooling where real-time responsiveness to file updates is beneficial.
One effective way to achieve this in Python is by using the watchdog library which is a simple and efficient tool that listens for file system events like creation, modification, and deletion.
With watchdog library the developers can set up observers that watch specific directories and respond to the changes through event handlers. For instance, when a .py file is edited or saved then the system can automatically log the change, restart a server, or initiate any custom action that we define and this eliminates the need for manual restarts or checks by improving productivity and reliability.
Installing Watchdog
Before using the Watchdog library we need install in our system by using the below command -
pip install watchdog
Create a Python Script to Watch Files
Below is an example that monitors a directory for changes to .py files and prints a message when they are modified, created or deleted -
import watchdog from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler class FileChangeHandler(FileSystemEventHandler): def on_modified(self, event): if not event.is_directory: print(f"File {event.src_path} has been modified!") def monitor_file_changes(directory, file_extension): event_handler = FileChangeHandler() observer = Observer() observer.schedule(event_handler, path=directory, recursive=True) observer.start() try: while True: pass except KeyboardInterrupt: observer.stop() observer.join()
Exercising Fine-tuned Control with the watchdog.events Module
watchdog.events Module is a core part of the Python watchdog library by offering event handler classes to help us to define custom responses to file system events such as file creation, modification, etc.
Example
Here is an example that deals with the process of file monitoring by employing the watchdog.events module directly allowing for granular control over file events -
import watchdog from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler, EVENT_TYPE_MODIFIED class FileChangeHandler(FileSystemEventHandler): def on_modified(self, event): if not event.is_directory and event.event_type == EVENT_TYPE_MODIFIED: print(f"File {event.src_path} has been modified!") def monitor_file_changes(directory, file_extension): event_handler = FileChangeHandler() observer = Observer() observer.schedule(event_handler, path=directory, recursive=True) observer.start() try: while True: pass except KeyboardInterrupt: observer.stop() observer.join()
Embracing the Polling Approach
In a watchdog setup, the polling approach means the watchdog regularly checks i.e., polls the status of the thing it's monitoring such as -
- Is the process still running?
- Is the disk usage below threshold?
- Is the service responding to health checks?
Without waiting for the monitored system to notify of an issue, the watchdog proactively checks at intervals.
Example
Following is an example which deals with the file monitoring, deploying the polling approach, and wherein we engage in periodic checks for file changes.
import time import os def monitor_file_changes(directory, file_extension): file_paths = [os.path.join(directory, file) for file in os.listdir(directory) if file.endswith(file_extension)] while True: for file_path in file_paths: current_timestamp = os.path.getmtime(file_path) if file_path in monitored_files and monitored_files[file_path] != current_timestamp: print(f"File {file_path} has been modified!") monitored_files[file_path] = current_timestamp time.sleep(1)
Unveiling the inotify Library (Exclusive to Linux)
Inotify is abbreviated as inode notify. This library is a Linux kernel subsystem that provides a way for applications to monitor changes to the filesystem in real time.
The following is an example that showcases file monitoring, where we deploy the inotify library and which is available solely on Linux systems.
import inotify.adapters def monitor_file_changes(directory, file_extension): i = inotify.adapters.Inotify() i.add_watch(directory) for event in i.event_gen(): if event is not None: (header, type_names, path, filename) = event if file_extension in filename: print(f"File {filename} has been modified!")