pythonfileprocesswatchdog

Check files modification from a process with Python watchdog


I'm trying to use Python watchdog and I don't know if it's possible to check if a process modified/created/deleted files, can anyone help me?

import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler


logging.basicConfig(level=logging.INFO, format=' %(message)s')
path = sys.argv[1] if len(sys.argv) > 1 else '.'

event_handler = LoggingEventHandler()

observer = Observer()
observer.schedule(event_handler, path, recursive=True)

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

How can I change the code to show the process that changed files in a specific folder?


Solution

  • How can I change the code to show the process that changed files in a specific folder?

    You can't :(

    Observer class is using scandir for modified/created/deleted files/folders and that's all. To get the information about which process is modifying a file, you probably need to use same OS specyfic tools like Windows system access control list or linux auditctl