watchfiles类-watch功能:监控指定目录文件变化,并进行通知。
from watchfiles import watch
for changes in watch('./'):
print(changes)
打印当前目录更新的文件。
import threading
from watchfiles import watch
class Test:
def __init__(self,path):
self.event = threading.Event()
self.watcher = watch(path,stop_event=self.event,yield_on_timeout=True)
def run(self):
print("--start--")
for path in self:
print("path:",path)
if path:
print("stoping")
self.stop()
print("--end--")
def __iter__(self):
return self
def __next__(self):
return self.get()
def get(self):
return next(self.watcher)
def stop(self):
print("--stop--")
self.event.set()
Test("./").run()
以上为 封装成class的调用方式。