在python中 文件监控主要有两个库,

一个是pyinotify ( https://github.com/seb-m/pyinotify/wiki ),pyinotify依赖于Linux平台的inotify。

一个是watchdog(http://pythonhosted.org/watchdog/)。对不同平台的的事件都进行了封装。

1、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()
event_handler = FileEventHandler()
observer.schedule(event_handler,"d:/dcm",True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()

2、pyinotify  

  http://outofmemory.cn/code-snippet/84447/Linux-environment-jiankong-directory-variety-Python-code-segment

http://www.cppcns.com/jiaoben/python/125108.html

Linux 目录变化监听 - python代码实现的更多相关文章

  1. linux epoll机制对TCP 客户端和服务端的监听C代码通用框架实现

    1 TCP简介 tcp是一种基于流的应用层协议,其“可靠的数据传输”实现的原理就是,“拥塞控制”的滑动窗口机制,该机制包含的算法主要有“慢启动”,“拥塞避免”,“快速重传”. 2 TCP socket ...

  2. linux实时文件事件监听--inotify

    一.inotify简介 inotify是Linux内核2.6.13 (June 18, 2005)版本新增的一个子系统(API),它提供了一种监控文件系统(基于inode的)事件的机制,可以监控文件系 ...

  3. Vue中使用computed与watch结合实现数据变化监听

    目的:当数据变化时,为其中重要数据增加边框,实现闪烁以达到提醒目的.数据格式如下,只有在未处理火警/故障时增加闪烁边框.可以使用watch进行深度监听.数据格式已定,也非常明确要监听的数据是有两个.既 ...

  4. linux 开启oracle监听

    secureCRT连接到数据库所在的linux机器,切换到oracle用户模式下 [root@nstlbeta ~]# su - oracle 步骤阅读 2 然后用sqlplus登录到数据库,关闭数据 ...

  5. LINUX启动ORACLE监听和服务

    可通过secureCRT或者telnet直接连 启动监听命令:lsnrctl start 成功启动后:sqlplus /nolog 回车 conn / as sysdba 回车 startup 回车 ...

  6. Android软键盘的隐藏显示、事件监听的代码

    把开发过程中重要的一些内容片段做个珍藏,如下资料是关于Android软键盘的隐藏显示.事件监听的内容,应该是对小伙伴们有所用途. public class ResizeLayout extends L ...

  7. angular5 路由变化监听

    1.路由监听 //监听路由变化this.router.events .filter(event => event instanceof NavigationEnd) .map(() => ...

  8. FocusChange-焦点变化监听事件

    想要监听一个控件的焦点变化情况,发现了一个 view.setOnFocusChangeListener(new OnFocusChangeListener() { ...... } 现在写一个小dem ...

  9. 了解linux web的监听工具

    zabbix cacti Nagios 本想安装的,但是安装需要一个 空的服务器,因为服务器已经有安装 LAMP,故没有去了解 尝试了 cacti ,因为本地环境版本问题,只能使用0.8.8a版本,并 ...

随机推荐

  1. R 《回归分析与线性统计模型》page140,5.1

    rm(list = ls()) library(car) library(MASS) library(openxlsx) A = read.xlsx("data140.xlsx") ...

  2. HTML中用自定义字体实现小图标icon(不是原作, 只是一个研究笔记)

    最近在做一个项目时, 研究了一下新浪微博的前端, 看到首页中那个图标了吗, 以前看到这类效果的第一反应就是用一个gif之类的图标做出来!! 但在研究的过程, 发现了一个小技巧, 注意那个em标签中的文 ...

  3. 009.Oracle数据库 , between关键字判断日期在两者之间

    /*Oracle数据库查询日期在两者之间*/ SELECT PKID, OCCUR_DATE, ATA FROM LM_FAULT WHERE ( OCCUR_DATE BETWEEN to_date ...

  4. Ubuntu 14.04 配置 VNC Server

    用putty连接Linux后,如果会话断开,也会终止此会话在Linux执行的任务. 用WinSCP传输文件很方便,目前也只能传输文件. 按照以下步骤以及提示,安装VNC Server, 1.apt-g ...

  5. 获取目录结构,并写到txt文档里

    cmd里直接运行: tree /f > ml.txt 写成bat tree D:\a\ /f > D:\a\目录.txt 效果 卷 本地磁盘 的文件夹 PATH 列表 卷序列号为 18A9 ...

  6. JuJu alpha

    JuJu alpha阶段总结博客 JuJu   设想与目标   我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 在cao ying researcher给的资料中定 ...

  7. MongoDB_04_插入和查询

    案列需求: 存在文章评论的数据存放到MongoDB中,数据结构参考如下: 数据库:articledb 专栏文章评论 comment / / 字段名称 字段含义 字段类型 备注 _id ID Objec ...

  8. S7-300 实训3 异步电机正反转控制

    含有视频 方便以后查阅 参考书籍 跟我动手学 S7-300/400 PLC 第2版  廖常初 主编 实训3 异步电动机 正反转控制 步骤1 步骤2 在 cycle execution 前方 右击 插入 ...

  9. POJ1014:Dividing

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63013   Accepted: 16315 Descri ...

  10. C++(五)构造函数

    //构造函数的作用:就是在函数被创建时使用特定的值构造对象,将对象初始化为一个特定的初始状态//例如在构造一个clock类对象的时候,将初始的时间设定为0:0:0//构造函数的名必须与类名相同,不能定 ...