您现在的位置是:网站首页 > 博客日记 >

python配置文件实时监控

作者:YXN-python 阅读量:17 发布日期:2024-11-06

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


class __FileWatchdog(FileSystemEventHandler):
    def on_modified(self, event):
        print(f'文件被修改: {event.src_path}')

    def on_created(self, event):
        print(f'文件被创建: {event.src_path}')

    def on_deleted(self, event):
        print(f'文件被删除: {event.src_path}')


def __start_watchdog():
    event_handler = __FileWatchdog()
    observer = Observer()
    observer.schedule(event_handler, path='.', recursive=False)
    observer.start()

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


threading.Thread(target=__start_watchdog).start()

YXN-python

2024-11-06