Python中使用Inotify监控文件实例
作者:junjie 时间:2021-03-03 14:17:05
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,Inotify,监控文件
0
投稿
猜你喜欢
一个简单的 js 上滚信息栏
2013-08-07 03:26:30
HTML5 Canvas 起步(3) - 颜色与渐变
2009-06-08 12:58:00
JavaScript实现简易轮播图最全代码解析(ES5)
2024-04-16 10:40:22
python处理“&#”开头加数字的html字符方法
2021-11-05 17:09:42
Sqlserver 高并发和大数据存储方案
2024-01-17 22:45:56
Python实现指定数组下标值正序与倒序排序算法功能举例
2023-04-03 10:37:26
python实现websocket的客户端压力测试
2023-09-30 11:19:57
MS SQL SERVER 数据库日志压缩方法与代码
2024-01-13 04:24:00
Python urlopen()函数 示例分享
2022-08-15 01:00:58
浅析MysQL B-Tree 索引
2024-01-17 00:20:44
Python-Selenium自动化爬虫
2021-04-04 10:55:39
Python 字符串转换为整形和浮点类型的方法
2021-09-02 00:09:31
Python入门教程4. 元组基本操作 <font color=red>原创</font>
2021-01-12 09:53:21
fso怎样判断一个盘上是否有文件
2007-09-26 12:35:00
nlp计数法应用于PTB数据集示例详解
2023-10-26 17:24:07
SQL语句练习实例之一——找出最近的两次晋升日期与工资额
2024-01-19 06:19:50
Python 随机生成中文验证码的实例代码
2022-12-15 23:17:34
Oracle性能究极优化 上第1/2页
2010-07-30 13:26:00
MySQL开启Slow慢查询的方法示例
2024-01-14 17:43:26
python opencv 批量改变图片的尺寸大小的方法
2021-09-13 12:55:01