[Python] Monitoring subdirectories on a network drive with python watchdog module

Discussão em 'Python' iniciado por Stack, Setembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    I'm using this code to monitor the creation of new files in a directory in my network drive and having issues, when I test the same code on a directory in the local drive it has no issues. When a new file is created in the directory it triggers the event and sends a message to the console, if a file is created in a subdirectory of that it also triggers an event and sends a message.

    But when I change the path back to the directory in the network drive, if the schedule has recursion=True than no events are triggered in the directory or subdirectory, if recursion=False events are only triggered in the scheduled directory.

    The end goal for this is to constantly monitor the network drive and anytime a new file is uploaded to a subdirectory of the directory being watched it'll send the file to the corresponding directory of another drive via FTP. Please let me know if there is an issue I'm missing with Watchdog or if there is a better way to accomplish my goal.

    import sys
    from time import sleep
    from watchdog.events import FileSystemEventHandler
    from watchdog.observers.polling import PollingObserver
    from pathlib import Path

    class MyEventHandler(FileSystemEventHandler):
    def on_created(self, event):
    path1=Path(event.src_path)
    parpath=path1.parent
    print("New file created in " + str(parpath))
    return super().on_created(event)


    if __name__ == "__main__":
    path = /network/drive

    event_handler = MyEventHandler()
    observer = PollingObserver()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
    while True:
    time.sleep(1)
    except KeyboardInterrupt:
    observer.stop()
    observer.join()

    Continue reading...

Compartilhe esta Página