[Python] Avoid Python Watchdog Script Crash on Error

Discussão em 'Python' iniciado por Stack, Julho 21, 2025.

  1. Stack

    Stack Membro Participativo

    I run a Python program based on the package watchdog which looks for new files to be created in a folder and will execute a function for each new file (simplified sample code):

    import watchdog.events
    import watchdog.observers

    class Handler(watchdog.events.PatternMatchingEventHandler):
    def __init__(self):
    watchdog.events.PatternMatchingEventHandler.__init__(self, patterns=['*.csv'],
    ignore_directories=True, case_sensitive=False)

    def on_created(self, event):
    # Code section A
    print('Event detected!')
    filename = os.path.basename(event.src_path)
    result = my_function(filename)

    if __name__ == "__main__":
    src_path = connector_path + 'data/2_prepared'
    event_handler = Handler()
    observer = watchdog.observers.Observer()
    observer.schedule(event_handler, path=src_path, recursive=True)
    observer.start()
    try:
    while True:
    time.sleep(1)
    except KeyboardInterrupt:
    observer.stop()
    observer.join()


    Suprisingly, when I run this script and an error occurs in my_function() then watchdog will stop working, i. e. future events will not execute code section A anymore until I restart the program. The structure of the program suggests that code section A will be executed whenever an event occurs and anything outside of code section A will be unaffected but this doesn't seem to be true.

    So the next question is of course: How do I handle this? How do I tell watchdog to keep going (or just restart after an error, for that matter)?

    Continue reading...

Compartilhe esta Página