文件名称:python watchdog 检测文件变化的小助手
文件大小:5.95MB
文件格式:EXE
更新时间:2021-11-26 11:12:23
Py watchdog
输入需要被监视的文件夹路径,自动创建D:/test文件夹.被监视文件夹有创建新文件时会同步复制到D:/test文件夹下 import time import os,shutil from watchdog.observers import Observer from watchdog.events import PatternMatchingEventHandler i=input("输入文件夹的位置:") if os.path.exists('d:/test'): print("环境已存在!") else: os.makedirs(r"d:\test") print("环境布置成功!") watch_patterns = "*.exe" # 监控文件的模式 ignore_patterns = "" # 设置忽略的文件模式 ignore_directories = True # 是否忽略文件夹变化 case_sensitive = True # 是否对大小写敏感 event_handler = PatternMatchingEventHandler(watch_patterns, ignore_patterns, ignore_directories, case_sensitive) def on_modified(event): # print("{0} 修改".format(event.src_path)) pash = event.src_path.replace(".crdownload","") if os.access(pash,os.X_OK): time.sleep(1) shutil.copy(pash, "D:\\test") print("成功") else: time.sleep(3) shutil.copy(pash, "D:\\test") event_handler.on_modified = on_modified watch_path = i # 监控目录 go_recursively = True # 是否监控子文件夹 my_observer = Observer() my_observer.schedule(event_handler, watch_path, recursive=go_recursively) my_observer.start() try: while True: time.sleep(2) except KeyboardInterrupt: my_observer.stop() my_observer.join()