wbyixx

介绍

watchdog用来监控指定目录/文件的变化,如添加删除文件或目录、修改文件内容、重命名文件或目录等,每种变化都会产生一个事件,且有一个特定的事件类与之对应,
然后再通过事件处理类来处理对应的事件,怎么样处理事件完全可以自定义,只需继承事件处理类的基类并重写对应实例方法。

学习资源

文档地址:https://pythonhosted.org/watchdog/
github地址:https://github.com/gorakhargosh/watchdog

博客参考:
https://blog.csdn.net/chdhust/article/details/50514391

基本使用

import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format=\'%(asctime)s - %(message)s\',
                        datefmt=\'%Y-%m-%d %H:%M:%S\')
    path = sys.argv[1] if len(sys.argv) > 1 else \'.\'
    event_handler = LoggingEventHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

分类:

技术点:

相关文章:

  • 2022-02-02
  • 2021-11-02
  • 2021-12-01
  • 2022-12-23
  • 2021-08-03
  • 2021-10-07
  • 2022-03-06
  • 2022-12-23
猜你喜欢
  • 2022-01-26
  • 2022-12-23
  • 2022-01-21
  • 2021-11-08
  • 2021-11-08
  • 2021-11-08
相关资源
相似解决方案