I would like run my python script once a file in a certain folder is modified. To do this, I chose Watchdog.
First, I created an own project folder on my local machine and created and activated a venv. The following python script is also in the same folder which gets monitored.
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import pandas as pd
def on_modified(event):
print("modified")
test()
def test():
print("test")
if __name__ == "__main__":
path = 'C:/Users/MyUser/Desktop/Watchdog_test'
event_handler = FileSystemEventHandler()
event_handler.on_modified = on_modified
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
print("monitoring")
while True:
time.sleep(3)
except KeyboardInterrupt:
observer.stop()
print("Done")
observer.join()
This is a easy example code but it shows the issue I have. So the code basically works and once I modify a file in the folder, I get "modified" in the terminal. Unfortunately, this does not trigger the function test(). In my real code I do the same but then trigger another function with triggers each function, like:
def on_modified(event):
print("modified")
trigger_all_functions()
def trigger_all_functions():
function1(...)
... = function2(...)
and so on