Python doesn't recognize Watchdog

13 Views Asked by At

I created a program in python that logs any deletion activity in the Windows recycle bin, and writes it to a log.txt file. Only problem, the computer does not recognize watchdog as a module for me. Otherwise, the code seems to me to be completely correct:

import os
import time
from watchdog.observer import Observer
from watchdog.events import FileSystemEventHandler

class MyHandler(FileSystemEventHandler):
    def on_any_event(self, event):
        if event.is_directory:
            return
        
        action = ""
        if event.event_type == 'created':
            action = "CREATED"
        elif event.event_type == 'modified':
            action = "MODIFIED"
        elif event.event_type == 'deleted':
            action = "DELETED"
        else:
            action = "UNKNOWN"
        
        log_file_path = r"C:\Users\<my name>\Desktop\BinWatchdog\log.txt"
        with open(log_file_path, 'a') as f:
            f.write(f"{action}: {event.src_path}\n")

if __name__ == "__main__":
    path = 'C:\$Recycle.bin'
    
    event_handler = MyHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    
    print(f"Monitoring folder: {path}")
    
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

First, I tried uninstalling and reinstalling watchdog from cmd in admin, but nothing changed. Next, I also tried putting the folder where watchdog was in the path but nothing, I really don't know how to do that. If anyone out there has the solution I would be very very grateful. Thank you very much in advance!

Ps: sorry for the bad english

0

There are 0 best solutions below