

# https://www.globalsino.com/ICs/page4853.html
# Program function: monitor the change in a specific folder

import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import datetime as dt
import time

print('To stop the program, press Control_C.')

class OnMyWatch:
    # Directory on watch
    watchDirectory = r"C:\Users\yyliao\Downloads"
  
    def __init__(MyChecking):
        MyChecking.observer = Observer()
  
    def run(MyChecking):
        event_handler = Handler()
        MyChecking.observer.schedule(event_handler, MyChecking.watchDirectory, recursive = True)
        MyChecking.observer.start()
        try:
            while True:
                time.sleep(2)
        except:
            MyChecking.observer.stop()
            print("Observer Stopped")
  
        MyChecking.observer.join()  
  
class Handler(FileSystemEventHandler):
  
    @staticmethod
    def on_any_event(event):
        if event.is_directory:
            return None  
        elif event.event_type == 'created':
            # Event is created
            print(dt.date.today(), time.asctime(), event.src_path)
        elif event.event_type == 'modified':
            # Event is modified
            print(dt.date.today(), time.asctime(), event.src_path)              
  
if __name__ == '__main__':
    watch = OnMyWatch()
    watch.run()

