python watchdog monitoring file for changes -
folks, have need watch log file changes. after looking through stackoverflow questions, see people recommending 'watchdog'. i'm trying test, , not sure add code when files change:
#!/usr/bin/python import time watchdog.observers import observer watchdog.events import loggingeventhandler if __name__ == "__main__": event_handler = loggingeventhandler() observer = observer() observer.schedule(event_handler, path='.', recursive=false) observer.start() try: while true: time.sleep(1) else: print "got it" except keyboardinterrupt: observer.stop() observer.join()
where add "got it", in while loop if files have been added/changed?
thanks!
instead of loggingeventhandler
define handler:
#!/usr/bin/python import time watchdog.observers import observer watchdog.events import filesystemeventhandler class myhandler(filesystemeventhandler): def on_modified(self, event): print "got it!" 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()
on_modified
called when file or directory modified.
Comments
Post a Comment