Monitor files for changes with Python

Stokry - Oct 2 '20 - - Dev Community

Today I want to show how can you monitor changes of a directory with watchdog. It is a Python module that monitors our filesystem looking for any changes (like the creation, change, or deletion of a file or of a directory). When a change occurs, the watchdog reports it to us via specific event that we can handle. So for example, if you need to watch a log file for changes this module is perfect, or if you had a configuration file that is been changed you can also use this module to track that.

First of all, you need to install Watchdog via pip:

pip install watchdog
Enter fullscreen mode Exit fullscreen mode

After that we need to make import

import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
Enter fullscreen mode Exit fullscreen mode

After that we need to create an event handler, basically is the object that will be notified when something happens on the folder that we are monitoring. Also we need another object, known as the observer, that will monitor our filesystem, looking for changes that will be handled by the event handler.

if __name__ ==  "__main__":
   event_handler = MyHandler()
   observer = Observer()
   observer.schedule(event_handler,  path='C:\\Users\\Stokry\Desktop\\test',  recursive=False)
   observer.start()
Enter fullscreen mode Exit fullscreen mode

Now that we have created the handler and observer we need to write the code we want to run when the events occur.
Let’s start creating three different functions that will be used when a file is modified, created, or deleted. We can also add function when something is moved.

class  MyHandler(FileSystemEventHandler):
    def  on_modified(self,  event):
         print(f'event type: {event.event_type} path : {event.src_path}')
    def  on_created(self,  event):
         print(f'event type: {event.event_type} path : {event.src_path}')
    def  on_deleted(self,  event):
         print(f'event type: {event.event_type} path : {event.src_path}')
Enter fullscreen mode Exit fullscreen mode

Putting all together:

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

class  MyHandler(FileSystemEventHandler):
    def  on_modified(self,  event):
         print(f'event type: {event.event_type} path : {event.src_path}')
    def  on_created(self,  event):
         print(f'event type: {event.event_type} path : {event.src_path}')
    def  on_deleted(self,  event):
         print(f'event type: {event.event_type} path : {event.src_path}')

if __name__ ==  "__main__":
    event_handler = MyHandler()
    observer = Observer()
    observer.schedule(event_handler,  path='C:\\Users\\Stokry\Desktop\\test',  recursive=False)
    observer.start()

     try:
        while  True:
            time.sleep(1)
     except  KeyboardInterrupt:
        observer.stop()
     observer.join()
Enter fullscreen mode Exit fullscreen mode

Now we can test this out.
Let's say we delete the file, the output is like this:

event type: deleted path : C:\Users\Stokry\Desktop\test\log_old.txt
Enter fullscreen mode Exit fullscreen mode

Thank you all.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .