网址: 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. 数据类型之字符串(string)(三)

    其他操作 1.len():求序列长度,返回数字 a = 'my name is wang'len(a) 返回 15 空格也占一个位置. 2.+:连接2个字符串 >>> b = 'wh ...

  2. 以图搜图(demo创建流程)

    window10添加向量数据库以及调用 创建docker 1,在windows功能中打开Hyper-V 和 容器 2,进入https://www.docker.com/ ,下载windows版本进行安 ...

  3. WSL2安装nvm并配置npm镜像源

    1.下载安装脚本并执行 curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash 2.关闭命令行后 ...

  4. 在docker中导入python的包时ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory

    问题: ImportError: libGL.so.1: cannot open shared object file: No such file or directory ImportError: ...

  5. Centos7安装Docker 及 Docker-compose

    1.安装环境 此处在Centos7进行安装,可以使用以下命令查看CentOS版本 lsb_release -a 在 CentOS 7安装docker要求系统为64位.系统内核版本为 3.10 以上,可 ...

  6. C语言中关于宏定义的学习

    1.C语言中宏定义的使用 2.GCC官方文档 3.C语言宏定义的几个坑和特殊用法

  7. mapreduce和yarn集群

    mapreduce : 先分再合,分而治之 分布式计算概念: 计算方式,与集中式计算相对.将应用拆分成小的部分,分配给多台计算机处理,mapreduce是分布式的计算框架. MR的特点:易于编程,良好 ...

  8. 【26期】如何判断一个对象是否存活?(或者GC对象的判定方法)?

    这个问题,面试被问到的概率还是很大的.以下关于 如何判断一个对象是否存活 的回答,完全参照<深入理解Java虚拟机>一书,有需要的可以看书学习.以下是题目解析 判断对象是否存活的算法包括: ...

  9. goalng 将字符串转化成整数后取余

    package main import ( "fmt" "github.com/google/uuid" "hash/fnv" ) func ...

  10. HIVE- concat方法

    (1)concat_ws() select user ,concat_ws(';' , collect_set(cast(amt as string))) as amt from table grou ...