Python中使用Inotify监控文件实例


Posted in Python onFebruary 14, 2015

Inotify地址:访问

# -*- coding:utf-8 -*-

import os
import pyinotify
from functions import *

WATCH_PATH = '' #监控目录

if not WATCH_PATH:
  wlog('Error',"The WATCH_PATH setting MUST be set.")
  sys.exit()
else:
  if os.path.exists(WATCH_PATH):
    wlog('Watch status','Found watch path: path=%s.' % (WATCH_PATH))
  else:
    wlog('Error','The watch path NOT exists, watching stop now: path=%s.' % (WATCH_PATH))
    sys.exit()

class OnIOHandler(pyinotify.ProcessEvent):
  def process_IN_CREATE(self, event):
    wlog('Action',"create file: %s " % os.path.join(event.path,event.name))

  def process_IN_DELETE(self, event):
    wlog('Action',"delete file: %s " % os.path.join(event.path,event.name))

  def process_IN_MODIFY(self, event):
    wlog('Action',"modify file: %s " % os.path.join(event.path,event.name))

def auto_compile(path = '.'):
  wm = pyinotify.WatchManager()
  mask = pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_MODIFY
  notifier = pyinotify.ThreadedNotifier(wm, OnIOHandler())
  notifier.start()
  wm.add_watch(path, mask,rec = True,auto_add = True)
  wlog('Start Watch','Start monitoring %s' % path)
  while True:
    try:
      notifier.process_events()
      if notifier.check_events():
        notifier.read_events()
    except KeyboardInterrupt:
      notifier.stop()
      break

if __name__ == "__main__":
   auto_compile(WATCH_PATH)
Python 相关文章推荐
Python的高级Git库 Gittle
Sep 22 Python
Python实现过滤单个Android程序日志脚本分享
Jan 16 Python
Python wxpython模块响应鼠标拖动事件操作示例
Aug 23 Python
python编写计算器功能
Oct 25 Python
python实现将一维列表转换为多维列表(numpy+reshape)
Nov 29 Python
Python通过VGG16模型实现图像风格转换操作详解
Jan 16 Python
浅析matlab中imadjust函数
Feb 27 Python
浅谈PyTorch中in-place operation的含义
Jun 27 Python
Python使用sys.exc_info()方法获取异常信息
Jul 23 Python
Django修改app名称和数据表迁移方案实现
Sep 17 Python
Python selenium模拟网页点击爬虫交管12123违章数据
May 26 Python
Python实现科学占卜 让视频自动打码
Apr 09 Python
Python中实现的RC4算法
Feb 14 #Python
Python脚本实现网卡流量监控
Feb 14 #Python
Centos5.x下升级python到python2.7版本教程
Feb 14 #Python
Python脚本实现DNSPod DNS动态解析域名
Feb 14 #Python
Python压缩和解压缩zip文件
Feb 14 #Python
Python简单日志处理类分享
Feb 14 #Python
Python魔术方法详解
Feb 14 #Python
You might like
一个用于mysql的数据库抽象层函数库
2006/10/09 PHP
我的论坛源代码(三)
2006/10/09 PHP
PHP-Fcgi下PHP的执行时间设置方法
2013/08/02 PHP
PHP实现唤起微信支付功能
2019/02/18 PHP
基于jquery实现图片广告轮换效果代码
2011/07/07 Javascript
javascript学习笔记(十七) 检测浏览器插件代码
2012/06/20 Javascript
js函数在frame中的相互调用详解
2014/03/03 Javascript
IE6/IE7中JavaScript json提示缺少标识符、字符串或数字问题处理
2014/12/16 Javascript
JS中生成随机数的用法及相关函数
2016/01/09 Javascript
JQuery核心函数是什么及使用方法介绍
2016/05/03 Javascript
JavaScript计算值然后把值嵌入到html中的实现方法
2016/10/29 Javascript
vue组件Prop传递数据的实现示例
2017/08/17 Javascript
使用Angular material主题定义自己的组件库的配色体系
2019/09/04 Javascript
python 容器总结整理
2017/04/04 Python
Python 中 list 的各项操作技巧
2017/04/13 Python
python微信跳一跳系列之棋子定位像素遍历
2018/02/26 Python
python验证码识别教程之灰度处理、二值化、降噪与tesserocr识别
2018/06/04 Python
便捷提取python导入包的属性方法
2018/10/15 Python
python使用udp实现聊天器功能
2018/12/10 Python
Python实现监控Nginx配置文件的不同并发送邮件报警功能示例
2019/02/26 Python
Python 3.8中实现functools.cached_property功能
2019/05/29 Python
基于Python中isfile函数和isdir函数使用详解
2019/11/29 Python
Python搭建Keras CNN模型破解网站验证码的实现
2020/04/07 Python
Python私有属性私有方法应用实例解析
2020/09/15 Python
css3 仿写阿里云水纹效果的示例代码
2018/02/10 HTML / CSS
美国宠物商店:Wag.com
2016/10/25 全球购物
eBay法国购物网站:eBay.fr
2017/10/21 全球购物
星空联盟C# .net笔试题
2014/12/05 面试题
如何进行有效的自我评价
2013/09/27 职场文书
工商管理毕业生推荐信
2013/12/24 职场文书
艺术节主持词
2014/04/02 职场文书
环保倡议书300字
2014/05/15 职场文书
2014年反腐倡廉工作总结
2014/12/05 职场文书
优秀教师推荐材料
2014/12/16 职场文书
检讨书范文300字
2015/01/28 职场文书
python ConfigParser库的使用及遇到的坑
2022/02/12 Python