Skip to content

Instantly share code, notes, and snippets.

@njanakiev
Created February 19, 2018 11:10
Show Gist options
  • Save njanakiev/1ce90b29868a21083b0b8cae7a4cd4dc to your computer and use it in GitHub Desktop.
Save njanakiev/1ce90b29868a21083b0b8cae7a4cd4dc to your computer and use it in GitHub Desktop.
Simple example of monitoring file system events in Python with the watchdog module
import sys
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class EventHandler(FileSystemEventHandler):
def on_any_event(self, event):
print("EVENT")
print(event.event_type)
print(event.src_path)
print()
if __name__ == "__main__":
path = sys.argv[1] if len(sys.argv) > 1 else '.'
event_handler = EventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
@Vilangumthara
Copy link

can i give my path like this path = "T:\Laboratory\Instruments\Worklists\TrackMateRacks"
instead of path = sys.argv[1] if len(sys.argv) > 1 else '.'

@kitattyor
Copy link

can i give my path like this path = "T:\Laboratory\Instruments\Worklists\TrackMateRacks"
instead of path = sys.argv[1] if len(sys.argv) > 1 else '.'

Don't see why not. you'll need to escape the backslashes though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment