

# https://www.globalsino.com/ICs/
# Monitor file creation and then write the file paths into the text file


import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class Watcher:
  DIRECTORY_TO_WATCH = r"C:\0Python\images"

  def __init__(self):
    self.observer = Observer()

  def run(self):
    event_handler = Handler()
    self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
    self.observer.start()
    try:
      while True:
        time.sleep(5)
    except:
      self.observer.stop()
    self.observer.join()

class Handler(FileSystemEventHandler):
  @staticmethod
  def on_any_event(event):
    if event.is_directory:
      return None
    elif event.event_type == 'created':
      print(event.src_path)
      return run_function(event.src_path, theText)

theText =  r"C:\0Python\images\finalFolder\name_scripts\Layer1\Test.txt"

def run_function(TryToWriteTheWords, My_file_name):
  with open(My_file_name, "a+") as file_path:
    file_path.seek(0)
    # Append text at the end of file
    file_path.write(TryToWriteTheWords + "\n")
        
if __name__ == '__main__':
  theMonitor = Watcher()
  theMonitor.run()


