Stop monitoring a file with watchdog

362 views
Skip to first unread message

Joao Tomas

unread,
Apr 30, 2016, 6:49:41 PM4/30/16
to watchdog-python

I have this small script to monitoring with watchdog one single file (test.txt). Till now I got a screen message each time the file is modified but I need just the get the notification for the first time, it's mean to stop monitoring, is there any way I could tell watchdog to stop it?


Here is my code:

#!/usr/bin/python
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

file_to_scan = "test.txt"

class MyHandler(FileSystemEventHandler):    
    def on_modified(self, event):
        if file_to_scan in event.src_path:
                print "Got it!", event.src_path
                #### I want to stop here the monitoring

    def on_created(self, event):
        pass


if __name__ == "__main__":
    event_handler = MyHandler()
    observer = Observer()
    observer.schedule(event_handler, path ="." , recursive=False)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()
Reply all
Reply to author
Forward
0 new messages