Create a watchdog in Python to look for filesystem changes
Last Updated :
28 Feb, 2023
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 built-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.
Python3
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
if __name__ = = "__main__":
logging.basicConfig(level = logging.INFO,
format = '%(asctime)s - %(message)s' ,
datefmt = '%Y-%m-%d %H:%M:%S' )
path = sys.argv[ 1 ] if len (sys.argv) > 1 else '.'
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive = True )
observer.start()
try :
while True :
time.sleep( 1 )
except KeyboardInterrupt:
observer.stop()
observer.join()
|
Output: 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.
Python3
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class OnMyWatch:
watchDirectory = " / give / the / address / of / directory"
def __init__( self ):
self .observer = Observer()
def run( self ):
event_handler = Handler()
self .observer.schedule(event_handler, self .watchDirectory, recursive = True )
self .observer.start()
try :
while True :
time.sleep( 5 )
except :
self .observer.stop()
print ("Observer Stopped")
self .observer.join()
class Handler(FileSystemEventHandler):
@staticmethod
def on_any_event(event):
if event.is_directory:
return None
elif event.event_type = = 'created' :
print ("Watchdog received created event - % s." % event.src_path)
elif event.event_type = = 'modified' :
print ("Watchdog received modified event - % s." % event.src_path)
if __name__ = = '__main__' :
watch = OnMyWatch()
watch.run()
|
Output:
Deciphering the code
Observer
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.
Python3
import watchdog.events
import watchdog.observers
import time
class Handler(watchdog.events.PatternMatchingEventHandler):
def __init__( self ):
watchdog.events.PatternMatchingEventHandler.__init__( self , patterns = [ '*.csv' ],
ignore_directories = True , case_sensitive = False )
def on_created( self , event):
print ("Watchdog received created event - % s." % event.src_path)
def on_modified( self , event):
print ("Watchdog received modified event - % s." % event.src_path)
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 )
observer.start()
try :
while True :
time.sleep( 1 )
except KeyboardInterrupt:
observer.stop()
observer.join()
|
Output: 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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...