Python watchdog module on a mapped network drive directory and subdirecotry events

37 Views Asked by At

I have a mapped network drive on windows. I wish to monitor creation/modification/moving of new folders to this mapped network drive. I want to track InterOp (folder) and RunInfo.xml (file) for every run. If these files are present I want to generate metrics implementing another python library. For now I get the output when directory is created and modified under the 2024 folder but not under the subfolder.

Here's the code:

import sys 
import os 
import pandas as pd
from interop import py_interop_run_metrics, py_interop_run, py_interop_table, py_interop_summary, py_interop_metrics, py_interop_plot, py_interop_comm
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from watchdog.events import LoggingEventHandler
import logging

main_dir = "\\\\Path\\to\\mapped\\drive\\2024"
file_input_paths = []

class Handler(FileSystemEventHandler):
    def on_created(self, event):
        print(f"Direcotry {event.src_path} is created")

    def on_modified(self, event):
        print(f"Directory {event.src_path} is modified")
        file_input_paths.append(event.src_path)
        
observer=Observer()
event_handler=Handler()

print("Direcotory Created")
observer.schedule(event_handler, main_dir , recursive=True)

observer.start()

try: 
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    observer.stop()

observer.join()

With this code I am able to see created and modified folders under the 2024 folders. How can I track subfolder under this modified folder. Every week a new directory with sub directory is added to the 2024 mapped drive. Example, 01022024 Automated Run 1 is created under which 240104_M123_021_000000000-XXXX is moved from an instrument to mapped drive along with InterOp and RunInfo.xml present inside the 240104_M123_021_000000000-XXXX folder.

Here's example of directory structure which I want to track.

\\\\Path\\to\\mapped\\drive\\2024

-- 01022024 Automated Run 1
----- 240104_M123_021_000000000-XXXX
------------- InterOp
------------- RunInfo.xml

-- 01172024 Automated Run 2
----- 240115_M123_022_000000000-YYYY
------------- InterOp
------------- RunInfo.xml

-- 01172024 Automated Run 3
----- 240112_M789_001_000000000-ZZZZ
------------- InterOp
------------- RunInfo.xml

I ran observer.schedule() for each direcotry file_input_paths, however it does not work. How can I achieve this?

0

There are 0 best solutions below