Skip to content

Instantly share code, notes, and snippets.

@alfakini
Created October 16, 2018 00:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alfakini/2068679131598835add93575c85a3657 to your computer and use it in GitHub Desktop.
Save alfakini/2068679131598835add93575c85a3657 to your computer and use it in GitHub Desktop.
Filesystem events monitoring with Python [watcher.py]
import sys
import time
from watchdog.observers import Observer
from .events import ImagesEventHandler
class ImagesWatcher:
def __init__(self, src_path):
self.__src_path = src_path
self.__event_handler = ImagesEventHandler()
self.__event_observer = Observer()
def run(self):
self.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
self.stop()
def start(self):
self.__schedule()
self.__event_observer.start()
def stop(self):
self.__event_observer.stop()
self.__event_observer.join()
def __schedule(self):
self.__event_observer.schedule(
self.__event_handler,
self.__src_path,
recursive=True
)
if __name__ == "__main__":
src_path = sys.argv[1] if len(sys.argv) > 1 else '.'
ImagesWatcher(src_path).run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment