Watching a directory for files event in python

watchdog is a python package uses to notify linux kernel subsystem to watch for any changes to the filesystem. This is very handy package

Lets create a watcher using watchdog and save information to DB when any event occur.

1. Firstly I will write watcher class which look like

import time
import psycopg2
from watchdog.observers import Observer
from import FileSystemEventHandler
class Watcher:
    DIRECTORY_TO_WATCH = "/Users/abdullah/Development/personal/watcher-folder"
    def __init__(self): = Observer()
    def run(self):
        event_handler = Processor() // This will handle all event from watchdog, self.DIRECTORY_TO_WATCH, recursive=True)
            while True:
            print "Error"

In this class Processor will be a new class which will process all event which is coming from watchdog observer
Let’s write Processor class

class Processor(FileSystemEventHandler):
    def on_any_event(event):
        if event.is_directory:
            return None
            # Save to DB.
            Processor.save_to_db( event.src_path, event.event_type)
    def save_to_db(cls, file_path, action):
            conn = psycopg2.connect(host="localhost",database="watcher_db", user="admin", password="123456")
            sql = """INSERT INTO watcher(file_path, action)
                VALUES(%s, %s);"""
            cur = conn.cursor()
            # execute the INSERT statement
            cur.execute(sql, (file_path,action))
            # close communication with the database
        except (Exception, psycopg2.DatabaseError) as error:
            if conn is not None:

In the Processor class method on_any_event received event from observer when any event occurs on watching directory. I have skipped event processing when any directory changes. event will be processed or saved to DB only file event occurs like ‘created’, ‘modified’, ‘deleted’

So, let’s add final block to run this script

if __name__ == '__main__':
    w = Watcher()

** Before run this script you need to add table ‘watcher’ with column (id, file_path, action)
now let’s run script using


if you create any files on watching directory you will get that information to DB

To actually make this useful, you should wrap this up as a daemon or upstart script which can be run indefinitely.

Leave a Reply