Create a Watchdog in Python to Look for Filesystem Changes
A watchdog is a type of software that monitors the filesystem that has changed or not (like the creation, change or deletion of a file or of a directory). When we change something in the file system, the watchdog reports it to us raising a specific event that we can handle.
Consider an example of a program that uses a configuration file. The program could set a watchdog to monitor that file, and if the file configuration is changed, you need to reload it and apply the new configuration at runtime without restarting your program.
To create a watchdog in Python, you must install the watchdog module by typing the command watchdog.
$ pip install watchdog
So, after installing the watchdog module, you need to go to your editor and import some stuff.
Code:
import time
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
In the above code, we import the time module and the Observer class from the watchdog library that monitors the events of the file system, and the PatternMatchingEventHandler is the class from the watchdog library that defines event handlers based on file name patterns.
After importing the important modules, let's create the event handler; the event handler is the type of object that will be notified when there is something change in the filesystem that is monitored.
Code:
if __name__ == "__main__":
patterns = ["*"]
ignore_patterns = None
ignore_directories = False
case_sensitive = True
my_event_handler = PatternMatchingEventHandler(patterns, ignore_patterns, ignore_directories, case_sensitive)
Explanation:
In the above code, some variables are used to configure the event handler in a way that is easier to understand. The variable patterns consist of the file patterns we want to handle; in the above code, the '*' is used to tell all types of files that are handled, the variable 'ignore_pattern' variable contains the patterns that are not handled by the event handler, the variable ignore_directory is a type of Boolean variable that set to true if the regular files are to be notified or not, and the variable case_sensitive is another type of Boolean variable that if set to true made the patterns are case sensitive.
Let's make some functions to handle the events,
Code:
def on_created(event):
print(f"hey, {event.src_path} has been created!")
def on_deleted(event):
print(f" Someone deleted {event.src_path}!")
def on_modified(event):
print(f"hey buddy, {event.src_path} has been modified")
def on_moved(event):
print(f"ok ok ok, someone moved {event.src_path} to {event.dest_path}")
Explanation:
In the above code, several functions are created that do some tasks given to the function. The function on_created takes the argument as an event and prints the message that some event is created. The function on_delete also takes the argument as an event and gives the output as the same event is deleted. The function on modified also takes the argument as an event and prints the output that some event is modified, and last, the function on moved takes the argument as an event and prints the output that some event is deleted to its destination path.
Now, let's specify the handlers that we went to and the function to be handled or called when the corresponding event is raised.
Code:
my_event_handler.on_created = on_created
my_event_handler.on_deleted = on_deleted
my_event_handler.on_modified = on_modified
my_event_handler.on_moved = on_moved
Explanation:
In the above code, we can see that we have defined our functions to my_event_handler.
Now, after creating all the event handlers, create an observer that will monitor the file system and look for any change that will be handled by the event handler.
Code:
path = "."
go_recursively = True
my_observer = Observer()
my_observer.schedule(my_event_handler, path, recursive=go_recursively)
Output:
<ObservedWatch: path='.', is_recursive=True>
Explanation:
In the above code, an object of the Observer class is created and then called the schedule method passing to it.
- The event handler that handles the event.
- The path that is monitored.
- A Boolean variable that allows catching all the events that occur even in the sub-directories of the current directory.
Let's start with the observer who will monitor all the events and handle them.
Code:
my_observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
my_observer.stop()
my_observer.join()
Output:
hey, .\Untitled1.ipynb has been created!
hey buddy, .\Untitled1.ipynb has been modified
hey, .\.ipynb_checkpoints\Untitled1-checkpoint.ipynb has been created!
hey buddy, .\.ipynb_checkpoints\Untitled1-checkpoint.ipynb has been modified
hey buddy, .\.ipynb_checkpoints\Untitled1-checkpoint.ipynb has been modified
hey buddy, .\.ipynb_checkpoints has been modified
hey, .\.~Untitled3.ipynb has been created!
hey buddy, .\.~Untitled3.ipynb has been modified
hey buddy, .\.~Untitled3.ipynb has been modified
hey buddy, .\Untitled3.ipynb has been modified
hey buddy, .\Untitled3.ipynb has been modified
Someone deleted .\.~Untitled3.ipynb!
hey, .\.~Untitled1.ipynb has been created!
hey buddy, .\.~Untitled1.ipynb has been modified
hey buddy, .\.~Untitled1.ipynb has been modified
hey buddy, .\Untitled1.ipynb has been modified
hey buddy, .\Untitled1.ipynb has been modified
Someone deleted .\.~Untitled1.ipynb!
hey buddy, .\.ipynb_checkpoints\Untitled1-checkpoint.ipynb has been modified
hey buddy, .\.ipynb_checkpoints\Untitled1-checkpoint.ipynb has been modified
hey buddy, .\.ipynb_checkpoints\Untitled1-checkpoint.ipynb has been modified
Explanation:
In the above code, the observer is started, and the observer starts to monitor the event and print the event that occurs. In the output, a new file is created and modified so the handler gives the message.
Welcome to WordPress! This is your first post. Edit or delete it to take the first step in your blogging journey.