# https://www.globalsino.com/ICs/
# Monitor a specific file or files, and then execute the monitored file
# And then, restart the current file for new monitoring

import watchdog.events
import watchdog.observers
import time
import sys
from os.path import exists
import os

class Handler(watchdog.events.PatternMatchingEventHandler):
    def __init__(self):
        watchdog.events.PatternMatchingEventHandler.__init__(self, patterns=["Word.PNG"],
            ignore_directories=True, case_sensitive=False)

    def on_created(self, event):
        # Check creation of a file with a specific name ("Word.PNG").
        if exists(event.src_path) == True:
            print(event.src_path)            
            exec(open("page4853simpleCal.py",  encoding = 'utf-8').read())            
            
if __name__ == "__main__":
    path = r"C:\0Python\images\finalFolder"
    event_handler = Handler()
    observer = watchdog.observers.Observer()
    observer.schedule(event_handler, path=path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()
    os.execv(sys.executable, ["C:\GlobalSino2\ICs\page4853MonitorExecute.py"] + sys.argv)
