Windows OS: Watchdog python defines deleting a folder as deleting a file

70 views Asked by At

Decided to use the Watchdog library to keep track of folder changes in Windows. However, I discovered a very unfortunate problem. When defining the on_deleted function, I check the event via event.is_directory, but for some reason it fails the check and returns False. However, for the on_created function everything works fine. What is the problem? Maybe I am doing something wrong? python 3.11.5 watchdog 3.0.0

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class MyHandler(FileSystemEventHandler):
    def on_created(self, event):
        if event.is_directory:
            print(f"directory created: {event.src_path}")
        else:
            print(f"file created: {event.src_path}")
            
    
    def on_deleted(self, event):
        if event.is_directory:
            print(f"directory deleted: {event.src_path}")
        else:
            print(f"file deleted: {event.src_path}")

if __name__ == "__main__":
    event_handler = MyHandler()
    observer = Observer()
    local_path = "C:\\Users\\dekus\\OneDrive\\Desktop\\sohri"
    observer.schedule(event_handler, local_path, recursive=True)
    observer.start()
    observer.join()

Output

directory created: C:\Users\dekus\OneDrive\Desktop\sohri\Новая папка (5) --- for on_created it works fine
file deleted: C:\Users\dekus\OneDrive\Desktop\sohri\Новая папка (5) --- but for on_deleted...

0

There are 0 answers