python中文件变化监控-watchdog
2024-08-24 20:32:28
在python中文件监控主要有两个库,一个是pyinotify ( https://github.com/seb-m/pyinotify/wiki ),一个是watchdog(http://pythonhosted.org/watchdog/)。pyinotify依赖于Linux平台的inotify,后者则对不同平台的的事件都进行了封装。因为我主要用于Windows平台,所以下面着重介绍watchdog(推荐大家阅读一下watchdog实现源码,有利于深刻的理解其中的原理)。
watchdog在不同的平台使用不同的方法进行文件检测。在init.py中发现了如下注释:
|Inotify| Linux 2.6.13+ ``inotify(7)`` based observer
|FSEvents| Mac OS X FSEvents based observer
|Kqueue| Mac OS X and BSD with kqueue(2) ``kqueue(2)`` based observer
|WinApi|(ReadDirectoryChangesW) MS Windows Windows API-based observer
|Polling| Any fallback implementation
给出示例代码如下:
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):
if event.is_directory:
print("directory moved from {0} to {1}".format(event.src_path,event.dest_path))
else:
print("file moved from {0} to {1}".format(event.src_path,event.dest_path))
def on_created(self, event):
if event.is_directory:
print("directory created:{0}".format(event.src_path))
else:
print("file created:{0}".format(event.src_path))
def on_deleted(self, event):
if event.is_directory:
print("directory deleted:{0}".format(event.src_path))
else:
print("file deleted:{0}".format(event.src_path))
def on_modified(self, event):
if event.is_directory:
print("directory modified:{0}".format(event.src_path))
else:
print("file modified:{0}".format(event.src_path))
if __name__ == "__main__":
observer = Observer()
event_handler = FileEventHandler()
observer.schedule(event_handler,"d:/dcm",True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
watchdog主要采用观察者模型(废话,从变量命名就可以看出来)。主要有三个角色:observer,event_handler,被监控的文件夹。三者原本是独立的,主要通过observer.schedule函数将三者串起来,意思为observer不断检测调用平台依赖代码对监控文件夹进行变动检测,当发现改变时,通知event_handler处理。最后特别推荐读者有时间可以阅读一下watchdog的源码,写的易懂而且架构很好。
最新文章
- ASP.NET的新成员ASP.NET WebHooks
- [转载]TableView详解
- Oracle客户端与服务器字符集不统一的处理
- CentOS搭建LNMP环境
- HDU 4569 Special equations(数学推论)
- HTML图片热点、网页划区、拼接、表单
- [原创作品]观察者模式在Web App的应用
- 【leetcode列】3Sum
- js之date()对象
- 本科理工男如何学习Linux
- NMT 机器翻译
- 设计模式之备忘录模式(Memento )
- 大数据处理框架之Strom:认识storm
- sqlserver修改主机名
- Regression Analysis Using Excel
- C#中调用PowerShell代码
- 第33课 main函数与命令行参数
- 【字符串】manacher算法
- [Lua快速了解一下]Lua的OOP
- Centos6更新yum repo
热门文章
- window下遍历并修改文件
- 一劳永逸的搞定 FLEX 布局(转)
- Android JSON语法解析示例
- 设置RabbitMQ远程ip登录
- jquery validate使用笔记
- centos7下安装nmon后,无法运行,提示 cannot execute binary file或/lib64/ld64.so.1不存在
- Springmvc配置文件application.xml 和 spring-servlet.xml
- 静态类(static)与java值传递、引用传递小测
- django的queryset和objects对象
- PAT天梯赛L2-004 这是二叉搜索树吗【递归】