minte9
LearnRemember



Install Library

To trigger a Python script when a file is uploaded to a directory, you typically want to use a file system watcher that monitors the directory for any changes.
 
pip install watchdog

Trigger Script

Write a script that uses watchdog's FileSystemEventHandler to monitor a directory.
 
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class MyHandler(FileSystemEventHandler):

    def on_created(self, event):
        print(f'event type: {event.event_type}  path : {event.src_path}')

    def on_modified(self, event):
        print(f'event type: {event.event_type}  path : {event.src_path}')

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

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

observer.join()

"""
    event type: created  path : ./watchdog/file.txt
    event type: modified  path : ./watchdog
"""



  Last update: 109 days ago