码迷,mamicode.com
首页 > 编程语言 > 详细

Python-filewatchdog

时间:2020-01-02 16:07:22      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:try   ase   obs   The   keyboard   filesyste   des   执行   https   

安装watchdog包

pip install watchdog

 

 

import time
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler



#各种事件发生时执行的逻辑
def on_created(event):
    print(f"hey, {event.src_path} has been created!")

def on_deleted(event):
    print(f"what the f**k! Someone deleted {event.src_path}!")

def on_modified(event):
    print(f"hey buddy, {event.src_path} has been modified")

def on_moved(event):
    print(f"ok ok ok, someone moved {event.src_path} to {event.dest_path}")



if __name__ == "__main__":

    #监听类型
    patterns = "*"
    ignore_patterns = ""
    ignore_directories = False
    case_sensitive = True
    my_event_handler = PatternMatchingEventHandler(patterns, ignore_patterns, ignore_directories, case_sensitive)
    
    #每种事件执行的代码
    my_event_handler.on_created = on_created
    my_event_handler.on_deleted = on_deleted
    my_event_handler.on_modified = on_modified
    my_event_handler.on_moved = on_moved


    #监听路径
    path = r"D:/VSpython/json/"
    go_recursively = True
    my_observer = Observer()
    my_observer.schedule(my_event_handler, path, recursive=go_recursively)

    #开启监听
    my_observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        my_observer.stop()
        my_observer.join()

 

参考资料:https://www.thepythoncorner.com/2019/01/how-to-create-a-watchdog-in-python-to-look-for-filesystem-changes/

Python-filewatchdog

标签:try   ase   obs   The   keyboard   filesyste   des   执行   https   

原文地址:https://www.cnblogs.com/JinweiChang/p/12133110.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!