网址: https://blog.csdn.net/qq_40223983/article/details/102889329

起步
在python中文件监控主要有两个库,一个是pyinotify,一个是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

给出示例代码如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Created by victor

# 本模块的功能:<检测文件夹变化>

# 导入watchdog对应模块
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 = Observer()
event_handler = FileEventHandler()
# 设置监听目录
dis_dir = "e:/"
observer.schedule(event_handler,dis_dir,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的源码,写的易懂而且架构很好用

python监控文件变化的更多相关文章

  1. Python监控文件变化:watchdog

    Python监控文件变化有两种库:pyinotify和watchdog.pyinotify依赖于Linux平台的inotify,后者则对不同平台的的事件都进行了封装.也就是说,watchdog跨平台. ...

  2. JDK 之 NIO 2 WatchService、WatchKey(监控文件变化)

    JDK 之 NIO 2 WatchService.WatchKey(监控文件变化) JDK 规范目录(https://www.cnblogs.com/binarylei/p/10200503.html ...

  3. mac 监控文件变化并重启php

    自己撸一个框架,需要监控代码变化 安装fswatch brew install fswatch shell重启PHP脚本reload.sh #!/bin/sh do ps -ef | grep php ...

  4. python中文件变化监控-watchdog

    在python中文件监控主要有两个库,一个是pyinotify ( https://github.com/seb-m/pyinotify/wiki ),一个是watchdog(http://pytho ...

  5. linux 监控文件变化

    介绍 有时候我们常需要当文件变化的时候便触发某些脚本操作,比如说有文件更新了就同步文件到远程机器.在实现这个操作上,主要用到两个工具,一个是rsync,一个是inotifywait .inotifyw ...

  6. watchdog监控文件变化使用总结——转载

    原文链接地址:https://blog.csdn.net/xufive/article/details/93847372 概述 首先声明,本文讨论的 watchdog,不是单片机里的 watchdog ...

  7. inotifywait命令如何监控文件变化?

    转载自:https://segmentfault.com/a/1190000038351925 文件监控可以配合rsync实现文件自动同步,例如监听某个目录,当文件变化时,使用rsync命令将变化的文 ...

  8. Gulp-前端进阶A-3---如何不刷新监控文件变化?

    npm install --save-dev gulp-connect npm install --save-dev gulp-livereload npm其他,前面已有 var gulp = req ...

  9. 使用apache common-io 监控文件变化--转

    package common.io; import org.apache.commons.io.filefilter.FileFilterUtils; import org.apache.common ...

  10. 利用nodejs监控文件变化并使用sftp上传到服务器

    很久没写博客了,因为最近在用react+express做一个自己的工具型网站(其实就是夺宝岛抢拍器) 然后因为经常要改动,而且又要放到服务器上进行测试.总是要webpack,然后手动把文件上传上去,不 ...

随机推荐

  1. N63050 第十五周运维作业

    第二十九天: 网络文件共享服务 1基于DB数据库文件实现FTP的虚拟用户 2基于MySQL数据库文件实现FTP的虚拟用户 3NFS服务的工作原理 4NFS共享服务实现详解 5实现NFS共享存储的LAM ...

  2. WPF 布局控件

    <!--Horizontal水平竖直排放元素默认Vertical竖直排放 加属性Orientation--> <StackPanel Orientation="Horizo ...

  3. plugin的原理

    plugin插件的原理 扩展webpack, 加入自定义的构建行为 webpack内部的钩子 hooks tap: 可以注册同步钩子和异步钩子 tapAsync: 回调方式注册异步钩子 tapProm ...

  4. unity 阿拉伯数字转中文汉字

    直接调用即可 代码如下: using System; using System.Collections; using System.Collections.Generic; using System. ...

  5. 注意注意!!!!关于keil的问题,调试时候的bug

    1.keil变量不区分大小写: 2.KEIL调试debug时误报,未定义某变量 但是实际定义了的. 今天用keil写代码 感觉逻辑上没问题 但是始终不是那个效果 检查了半天错误问题.最后debug居然 ...

  6. 用python从网页下载单词库

    从网站下载单词库 1 每一页有几百个单词 2 每一个单词有独立的URL,URL中包含单词的中文解释 3 使用的库 requests,pyquery,web #coding:utf-8 import r ...

  7. idea等工具网盘下载地址

    1.idea2020 下载地址:https://caiyun.139.com/m/i?1E5C2SkIZbJH4 ,下载密码微信 搜索 "白菜拼吧" 回复 idea2020 获取 ...

  8. python菜鸟学习: 14. GUI界面化使用

    # 获取输入框中的内容 def getVars(): global outterDomain1, innertDomian1, guestEid1, appName1, unicodeName1, r ...

  9. Lubuntu 18.04 自动登录

    参考文章:https://blog.csdn.net/qq_20965753/article/details/61420431 sudo nano /usr/share/lightdm/lightdm ...

  10. 使用tkinter开发的一款登录和注册图形化界面

    目录 项目介绍 登录功能 登录界面展示 登录主要功能 登录部分源码 注册功能 注册界面展示 注册主要功能 注册部分源码 源码地址 项目介绍 使用tkinter开发的一款登录和注册图形化界面 使用tki ...