- # -*- 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中使用Inotify监控文件实例
2019-11-20 22:26junjie Python
这篇文章主要介绍了Python中使用Inotify监控文件实例,本文直接给出实现代码,需要的朋友可以参考下
延伸 · 阅读
- 2022-06-28Python数据读写之Python读写CSV文件
- 2022-06-28Python操作HDF5文件示例
- 2022-06-28python操作csv格式文件之csv.DictReader()方法
- 2022-06-28Python简明讲解filter函数的用法
- 2022-06-28分享13个非常有用的Python代码片段
- 2022-06-28使用Docker制作Python环境连接Oracle镜像
- Python
Django自带的用户验证系统实现
这篇文章主要介绍了Django自带的用户验证系统实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...
- Python
Python实现敏感词过滤的4种方法
这篇文章主要介绍了Python实现敏感词过滤的4种方法,帮助大家处理不和谐的言论,感兴趣的朋友可以了解下...
- Python
python中使用smtplib和email模块发送邮件实例
python脚本发邮件,一般会用到smtplib和email这两个模块。看看该模块怎么使用,先看smtplib模块。 smtplib模块定义了一个简单的SMTP客户端,可以用来在互联网上...
- Python
Windows下Python的Django框架环境部署及应用编写入门
这篇文章主要介绍了Windows下Python的Django框架环境部署及程序编写入门,Django在Python的框架中算是一个重量级的MVC框架,本文将从程序部署开始讲到hellow world...
- Python
python访问类中docstring注释的实现方法
这篇文章主要介绍了python访问类中docstring注释的实现方法,涉及python类注释的访问技巧,非常具有实用价值,需要的朋友可以参考下...
- Python
分析并输出Python代码依赖的库的实现代码
这篇文章主要介绍了分析并输出Python代码依赖的库的实现代码,需要的朋友可以参考下...
- Python
keras.layer.input()用法说明
这篇文章主要介绍了keras.layer.input()用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...
- Python
pandas将DataFrame的列变成行索引的方法
下面小编就为大家分享一篇pandas将DataFrame的列变成行索引的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...