1

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()
John Lapoya
  • 582
  • 1
  • 8
  • 21
  • 3
    Can you pass an observer to `MyHandler` and then call `self.observer.stop()` where you want to stop the monitoring? – mgilson Apr 30 '16 at 15:04
  • This answer has all the info you need: http://stackoverflow.com/questions/11883336/detect-file-creation-with-watchdog – Carlo Mazzaferro Jul 28 '16 at 17:49

0 Answers0