Watchdog module won’t notify me when a new file is added windows

  python, python-3.x, python-watchdog

I have looked for similar questions but i can’t find anything that has really helped. I’m trying to monitor a folder with Watchdog where every 13 seconds a new file is added and when Watchdog detects a new folder, the lector() function is triggerd to get the file name. But when i run the module and i add a new folder, the module won’t get triggered or show anything

This is the code i’m using:

class Watcher:
    DIRECTORY_TO_WATCH = path

    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()
            print('Error')

        self.observer.join()

# Lector de datos

def lector():

    datos = ""
    basepath = Path(path)
    files_in_basepath = basepath.iterdir()
    global data

    data = []

    for item in files_in_basepath:

        if item.is_file():
            datos = item.name

    with open(path + datos, 'r') as csvfile:

        lector = csv.reader(csvfile)

        for row in lector:

            data.append(row)

class Handler(FileSystemEventHandler):

    @staticmethod
    def on_any_event(event):
        if event.is_directory:

            lector()

        elif event.event_type == 'created' or event.event_type == 'modified':

            lector()

if __name__ == '__main__':
    w = Watcher()
    w.run()

Source: Python Questions

LEAVE A COMMENT