from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import time
class MyDirEventHandler(FileSystemEventHandler):
def on_moved(self, event):
print(event)
def on_created(self, event):
print(event)
def on_deleted(self, event):
print(event)
def on_modified(self, event):
print("modified:", event)
"""
使用watchdog 监控文件的变化
"""
if __name__ == '__main__':
observer = Observer()
fileHandler = MyDirEventHandler()
observer.schedule(fileHandler, "C:/test", True)
observer.start()
try:
while True:
time.sleep(2)
except KeyboardInterrupt:
observer.stop()
observer.join()