Skip to content
Related Articles

Related Articles

Improve Article

Create a watchdog in Python to look for filesystem changes

  • Last Updated : 06 Dec, 2019

Many times a file is needed to be processed at the time of its creation or its modification. This can be done by following changes in a particular directory. There are many ways in python to follow changes made in a directory. One such way is to use the watchdog module. As the name suggests this module observes the given directory and can notify if a file is created or changed.

Modules needed

  • Watchdog
    To install watchdog run this command in the terminal.
    pip install watchdog
  • Logging
    It comes buit-in with python, so there is no need to externally install it.

Now let’s dive into the code that will log all the changes registered. This code will log changes registered only in the current directory.

# import the modules
import sys
import time
import logging
from watchdog.observers import Observer
from import LoggingEventHandler
if __name__ == "__main__":
    # Set the format for logging info
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    # Set format for displaying path
    path = sys.argv[1] if len(sys.argv) > 1 else '.'
    # Initialize logging event handler
    event_handler = LoggingEventHandler()
    # Initialize Observer
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    # Start the observer
        while True:
            # Set the thread sleep time
    except KeyboardInterrupt:


The above code will log all the changes registered in a directory. If you want to make changes at the time a file is created or modified, you can do so by using the following code.

# import time module, Observer, FileSystemEventHandler
import time
from watchdog.observers import Observer
from import FileSystemEventHandler
class OnMyWatch:
    # Set the directory on watch
    watchDirectory = "/give / the / address / of / directory"
    def __init__(self): = Observer()
    def run(self):
        event_handler = Handler(), self.watchDirectory, recursive = True)
            while True:
            print("Observer Stopped")
class Handler(FileSystemEventHandler):
    def on_any_event(event):
        if event.is_directory:
            return None
        elif event.event_type == 'created':
            # Event is created, you can process it now
            print("Watchdog received created event - % s." % event.src_path)
        elif event.event_type == 'modified':
            # Event is modified, you can process it now
            print("Watchdog received modified event - % s." % event.src_path)
if __name__ == '__main__':
    watch = OnMyWatch()


Deciphering the code


The Observer is the class that watches for any file system change and then dispatches the event to the event handler. It monitors the file system and look for any changes.

Event Handler

The event handler is an object that will be notified when something happens to the file system. In general a script is written to watch over any type of new files created or modified like jpg, xml etc. For example, in the code below the PatternMatchingEventHandler inherits from the FileSystemEventHandler class and is used to do just that. Some useful methods of this class are:

  • on_any_event: will be executed for any event.
  • on_created: Executed when a file or a directory is created.
  • on_modified: Executed when a file is modified or a directory renamed.
  • on_deleted: Executed when a file or directory is deleted.
  • on_moved: Executed when a file or directory is moved.

Each one of those methods receives the event object as first parameter, and the event object has 3 attributes:

  • event_type: modified/created/moved/deleted
  • is_directory: True/False
  • src_path: path/to/observe/file

The below script is used to observe only .csv files using the PatternMAtchingEventHandler. You can further extend the patterns list if you want to observe more than one type of file.

import watchdog.observers
import time
class Handler(
    def __init__(self):
        # Set the patterns for PatternMatchingEventHandler, patterns=['*.csv'],
                                                             ignore_directories=True, case_sensitive=False)
    def on_created(self, event):
        print("Watchdog received created event - % s." % event.src_path)
        # Event is created, you can process it now
    def on_modified(self, event):
        print("Watchdog received modified event - % s." % event.src_path)
        # Event is modified, you can process it now
if __name__ == "__main__":
    src_path = r"C:\Users\GeeksforGeeks\PycharmProjects\requests hotel"
    event_handler = Handler()
    observer = watchdog.observers.Observer()
    observer.schedule(event_handler, path=src_path, recursive=True)
        while True:
    except KeyboardInterrupt:


Here with help of PatternMAtchingEventHandler we can take advantage of processing just events related with files with the .csv extension. By extending an event handler class provided by Watchdog we gain the ability to handle modified, created, deleted and moved events by implementing the class methods described above.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :