1、监控指定目录
from watchdog.observers import Observer
from watchdog.events import *
import time
class FileEventHandler(FileSystemEventHandler):
def __init__(self):
FileSystemEventHandler.__init__(self)
def on_moved(self,event):
now = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
if event.is_directory:
print(f"{ now } 文件夹由 { event.src_path } 移动至 { event.dest_path }")
else:
print(f"{ now } 文件由 { event.src_path } 移动至 { event.dest_path }")
def on_created(self,event):
now = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
if event.is_directory:
print(f"{ now } 文件夹由 { event.src_path } 创建")
else:
print(f"{ now } 文件由 { event.src_path } 创建")
def on_deleted(self,event):
now = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
if event.is_directory:
print(f"{ now } 文件夹 { event.src_path } 删除")
else:
print(f"{ now } 文件 { event.src_path } 删除")
def on_modified(self,event):
now = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
if event.is_directory:
print(f"{ now } 文件夹 { event.src_path } 修改")
else:
print(f"{ now } 文件 { event.src_path } 修改")
if __name__ == "__main__":
observer = Observer()
path = r"d:/test/"
event_handler = FileEventHandler()
observer.schedule(event_handler,path,True)
print(f"监控目录 { path }")
observer.start()
observer.join()
2、监控指定目录内指定的格式文档
from watchdog.observers import Observer
from watchdog.events import *
import time
class FileEventHandler(FileSystemEventHandler):
def __init__(self,control):
FileSystemEventHandler.__init__(self)
self.control = control
def on_moved(self,event):
now = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
if event.src_path.endswith(self.control):
print(f"{ now } 文件由 { event.src_path } 移动至 { event.dest_path }")
def on_created(self,event):
now = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
if event.src_path.endswith(self.control):
print(f"{ now } 文件由 { event.src_path } 创建")
def on_deleted(self,event):
now = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
if event.src_path.endswith(self.control):
print(f"{ now } 文件 { event.src_path } 删除")
def on_modified(self,event):
now = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
if event.src_path.endswith(self.control):
print(f"{ now } 文件 { event.src_path } 修改")
if __name__ == "__main__":
observer = Observer()
path = r"d:/test/"
event_handler = FileEventHandler('.docx')
observer.schedule(event_handler,path,True)
print(f"监控目录 { path }")
observer.start()
observer.join()
网友评论