watchdog示例:
import sys import time import logging from watchdog.observers import Observer from watchdog.events import LoggingEventHandler if __name__ == "__main__": # 配置日志 logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%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()