pyinotify简单实用(用于文件系统监控)
来源:互联网 发布:电影下载网站 知乎 编辑:程序博客网 时间:2024/06/05 11:32
Pyinotify是一个Python模块,用来监测文件系统的变化。 Pyinotify依赖于Linux内核的功能—inotify(内核2.6.13合并)。 inotify的是一个事件驱动的通知器,其通知接口通过三个系统调用从内核空间到用户空间。pyinotify结合这些系统调用,并提供一个顶级的抽象和一个通用的方式来处理这些功能。
pyinotify其实就是通过调用系统的inotify来实现通知的。
1. 安装
1
2
3
git clone https:
//github
.com
/seb-m/pyinotify
.git
cd
pyinotify/
python setup.py
install
2. 简单使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import
os
from
pyinotify
import
WatchManager, Notifier, ProcessEvent, IN_DELETE, IN_CREATE, IN_MODIFY
class
EventHandler(ProcessEvent):
def
process_IN_CREATE(
self
, event):
print
"Create file:%s."
%
os.path.join(event.path,event.name)
os.system(
'cp -rf %s /tmp/bak/'
%
(os.path.join(event.path,event.name)))
def
process_IN_DELETE(
self
, event):
print
"Delete file:%s."
%
os.path.join(event.path,event.name)
def
process_IN_MODIFY(
self
, event):
print
"Modify file:%s."
%
os.path.join(event.path,event.name)
def
FsMonitor(path
=
'.'
):
wm
=
WatchManager() #创建监视组
mask
=
IN_DELETE | IN_CREATE | IN_MODIFY
notifier
=
Notifier(wm, EventHandler()) #创建事件处理器,参数为监视组和对应的事件处理函数
wm.add_watch(path, mask, auto_add
=
True
, rec
=
True
) #将具体路径的监控加入监视组
print
"now starting monitor %s."
%
path
while
True
:
try
:
notifier.process_events() #对事件队列中的事件逐个调用事件处理函数
if
notifier.check_events(): #检查是否有新事件到来
print
"check event true."
notifier.read_events() #将新事件读入事件队列
except
KeyboardInterrupt:
print
"keyboard Interrupt."
notifier.stop() #停止对监视组事件的处理
break
if
__name__
=
=
"__main__"
:
FsMonitor(
"/root/work/"
)
0 0
- pyinotify简单实用(用于文件系统监控)
- Pyinotify文件系统监控
- Python pyinotify文件系统监控
- Pyinotify文件系统监控
- Pyinotify文件系统监控
- Python pyinotify文件系统监控
- Python下用于监视文件系统的pyinotify包
- pyinotify:在 Linux 中实时监控文件系统更改
- Inotify测试(2):inotifywait 与 PyInotify 监控层级比较
- hbase监控简单实用脚本
- 用于监控 Linux文件系统事件的inotify接口学习笔记
- Python pyinotify模块实现对文档的实时监控功能
- Inotify测试(1):PyInotify、Thread测试
- 文件系统监控
- 监控文件系统
- inotify机制简介(用于监控文件系统中文件及文件夹变化)
- 简单实用的容器监控命令——Ctop
- Python学习之pyinotify监控Linux下文件,并实现邮件报警
- f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\occcont.cpp(Line:950)错误
- JavaIo——运用RandomAccessFile实现文件的多线程下载
- 338. Counting Bits
- vim设置tab
- iOS入门之自定义tabBar及导航控制器浅谈
- pyinotify简单实用(用于文件系统监控)
- 剑指offer题解【替换空格】-java
- Tornado写简易服务器,转自ACdreamer
- MyBatis之自动生成实体类及映射文件《二》
- (英文)关于Unity3d c#和.a\.o\.so等native库文件的交互,库文件可以由c\c++\object-c等编译
- viewpager加小圆点
- windows环境配置 nginx + php + mysql
- 免费使用Windows版本的ZBrush的方法有哪些
- 调用相机拍照或者从图库里选择图片裁剪后设置到界面