watchdog


1. Monitor a Thread

import threading
import time
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler

def watch_thread(thread):
    class MyEventHandler(PatternMatchingEventHandler):
        patterns = ["*"]

        def on_modified(self, event):
            # Restart the thread if it's not running
            if not thread.is_alive():
                thread.start()

        def on_created(self, event):
            # Restart the thread if it's not running
            if not thread.is_alive():
                thread.start()

    observer = Observer()
    observer.schedule(MyEventHandler(), thread.name, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

thread = threading.Thread(target=my_function)
watch_thread(thread)

2. Monitor a File

3. Monitor a Directory

4. Monitor a Regex Pattern

5. Monitor Multiple Directories

6. Ignore Hidden Files and Directories

7. Monitor File Events and Errors

8. Monitor a File Handle

9. Monitor File Attributes

10. Monitor File Move Events