Python 的 pyinotify 模块 监控文件夹和文件的变动
官方参考:
https://github.com/seb-m/pyinotify/wiki/Events-types
https://github.com/seb-m/pyinotify/wiki/Install
最近在网上看到python有个pyinotify模块,其中他们可以监控文件夹内的文件的创建,修改,读取,删除等系列操作,我修改了下,添加了可以吧操作记录写到日志里的一点方法,下面就贴出代码了给大家分享下:#!/usr/bin/env python
import os
import datetime
import pyinotify
import logging
class MyEventHandler(pyinotify.ProcessEvent):
logging.basicConfig(level=logging.INFO,filename='/var/log/monitor.log')
#自定义写入那个文件,可以自己修改
logging.info("Starting monitor...") def process_IN_ACCESS(self, event):
print "ACCESS event:", event.pathname
logging.info("ACCESS event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now())) def process_IN_ATTRIB(self, event):
print "ATTRIB event:", event.pathname
logging.info("IN_ATTRIB event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now())) def process_IN_CLOSE_NOWRITE(self, event):
print "CLOSE_NOWRITE event:", event.pathname
logging.info("CLOSE_NOWRITE event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now())) def process_IN_CLOSE_WRITE(self, event):
print "CLOSE_WRITE event:", event.pathname
logging.info("CLOSE_WRITE event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now())) def process_IN_CREATE(self, event):
print "CREATE event:", event.pathname
logging.info("CREATE event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now())) def process_IN_DELETE(self, event):
print "DELETE event:", event.pathname
logging.info("DELETE event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now())) def process_IN_MODIFY(self, event):
print "MODIFY event:", event.pathname
logging.info("MODIFY event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now())) def process_IN_OPEN(self, event):
print "OPEN event:", event.pathname
logging.info("OPEN event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now())) def main():
# watch manager
wm = pyinotify.WatchManager()
wm.add_watch('/tmp', pyinotify.ALL_EVENTS, rec=True)
#/tmp是可以自己修改的监控的目录
# event handler
eh = MyEventHandler() # notifier
notifier = pyinotify.Notifier(wm, eh)
notifier.loop() if __name__ == '__main__':
main()
下面来看看效果如何呢:我在代码定义的是监控tmp目录下的变化:
[root@centos6 monitor-folder]# python total-monitor.py
OPEN event: /tmp/.ICE-unix
CLOSE_NOWRITE event: /tmp/.ICE-unix
OPEN event: /tmp
CLOSE_NOWRITE event: /tmp
OPEN event: /tmp
CLOSE_NOWRITE event: /tmp
DELETE event: /tmp/aa
DELETE event: /tmp/adduser.conf
DELETE event: /tmp/adjtime
DELETE event: /tmp/aliases
DELETE event: /tmp/bash.bashrc
DELETE event: /tmp/bindresvport.blacklist
DELETE event: /tmp/environment
DELETE event: /tmp/fstab
DELETE event: /tmp/ipt.err
DELETE event: /tmp/ipt.out
DELETE event: /tmp/krb5.conf
DELETE event: /tmp/odbc.ini
DELETE event: /tmp/odbcinst.ini
DELETE event: /tmp/timezone
DELETE event: /tmp/ucf.conf
DELETE event: /tmp/warnquota.conf
DELETE event: /tmp/wgetrc
DELETE event: /tmp/xinetd.conf
CREATE event: /tmp/aa
OPEN event: /tmp/aa
ATTRIB event: /tmp/aa
CLOSE_WRITE event: /tmp/aa
CREATE event: /tmp/bb
OPEN event: /tmp/bb
ATTRIB event: /tmp/bb
CLOSE_WRITE event: /tmp/bb
CREATE event: /tmp/cc
OPEN event: /tmp/cc
ATTRIB event: /tmp/cc
CLOSE_WRITE event: /tmp/cc 上面是打印出来的监控状态,下面是我的操作代码: [root@centos6 tmp]# ls
aa bash.bashrc ipt.err odbcinst.ini wgetrc
adduser.conf bindresvport.blacklist ipt.out timezone xinetd.conf
adjtime environment krb5.conf ucf.conf
aliases fstab odbc.ini warnquota.conf
[root@centos6 tmp]# rm -rf *
[root@centos6 tmp]# touch aa
[root@centos6 tmp]# touch bb
[root@centos6 tmp]# touch cc
[root@centos6 tmp]#
上面是直接打印出来的,下面在看看我吧操作记录在日志文件里面,也可以看下日志文件的记录:
INFO:root:Starting monitor...
INFO:root:OPEN event : /tmp/.ICE-unix 2011-10-27 13:31:57.219168
INFO:root:CLOSE_NOWRITE event : /tmp/.ICE-unix 2011-10-27 13:31:57.219609
INFO:root:OPEN event : /tmp/ 2011-10-27 13:32:21.395228
INFO:root:CLOSE_NOWRITE event : /tmp/ 2011-10-27 13:32:21.398105
INFO:root:OPEN event : /tmp/ 2011-10-27 13:32:25.108997
INFO:root:CLOSE_NOWRITE event : /tmp/ 2011-10-27 13:32:25.110239
INFO:root:CREATE event : /tmp/aa 2011-10-27 13:32:28.004863
INFO:root:OPEN event : /tmp/aa 2011-10-27 13:32:28.005860
INFO:root:IN_ATTRIB event : /tmp/aa 2011-10-27 13:32:28.006723
INFO:root:CLOSE_WRITE event : /tmp/aa 2011-10-27 13:32:28.014617
INFO:root:CREATE event : /tmp/bb 2011-10-27 13:32:30.149758
INFO:root:OPEN event : /tmp/bb 2011-10-27 13:32:30.164415
INFO:root:IN_ATTRIB event : /tmp/bb 2011-10-27 13:32:30.164877
INFO:root:CLOSE_WRITE event : /tmp/bb 2011-10-27 13:32:30.165303
INFO:root:CREATE event : /tmp/cc 2011-10-27 13:32:32.725418
INFO:root:OPEN event : /tmp/cc 2011-10-27 13:32:32.726367
INFO:root:IN_ATTRIB event : /tmp/cc 2011-10-27 13:32:32.727229
INFO:root:CLOSE_WRITE event : /tmp/cc 2011-10-27 13:32:32.735052
INFO:root:CREATE event : /tmp/dd 2011-10-27 13:32:39.771041
INFO:root:OPEN event : /tmp/dd 2011-10-27 13:32:39.780881
INFO:root:IN_ATTRIB event : /tmp/dd 2011-10-27 13:32:39.781455
INFO:root:CLOSE_WRITE event : /tmp/dd 2011-10-27 13:32:39.781893
INFO:root:Starting monitor...
INFO:root:OPEN event : /tmp/.ICE-unix 2011-10-27 14:01:43.742477
INFO:root:CLOSE_NOWRITE event : /tmp/.ICE-unix 2011-10-27 14:01:43.742915
INFO:root:OPEN event : /tmp/ 2011-10-27 14:01:50.579778
INFO:root:CLOSE_NOWRITE event : /tmp/ 2011-10-27 14:01:50.581317
INFO:root:DELETE event : /tmp/aa 2011-10-27 14:01:54.999528
INFO:root:DELETE event : /tmp/bb 2011-10-27 14:01:58.995966
INFO:root:DELETE event : /tmp/cc 2011-10-27 14:02:02.795950
INFO:root:DELETE event : /tmp/dd 2011-10-27 14:02:06.284208
INFO:root:OPEN event : /tmp/ 2011-10-27 14:02:07.738560
INFO:root:CLOSE_NOWRITE event : /tmp/ 2011-10-27 14:02:07.741922
INFO:root:CREATE event : /tmp/aa 2011-10-27 14:02:11.110322
INFO:root:OPEN event : /tmp/aa 2011-10-27 14:02:11.113150
INFO:root:IN_ATTRIB event : /tmp/aa 2011-10-27 14:02:11.116381
INFO:root:CLOSE_WRITE event : /tmp/aa 2011-10-27 14:02:11.118382
INFO:root:Starting monitor...
INFO:root:OPEN event : /tmp/.ICE-unix 2011-10-27 21:39:12.520432
INFO:root:CLOSE_NOWRITE event : /tmp/.ICE-unix 2011-10-27 21:39:12.520879
INFO:root:OPEN event : /tmp/ 2011-10-27 21:39:23.784759
INFO:root:CLOSE_NOWRITE event : /tmp/ 2011-10-27 21:39:23.793211
INFO:root:OPEN event : /tmp/ 2011-10-27 21:39:33.916232
INFO:root:CLOSE_NOWRITE event : /tmp/ 2011-10-27 21:39:33.916823
INFO:root:DELETE event : /tmp/aa 2011-10-27 21:39:33.939008
INFO:root:DELETE event : /tmp/adduser.conf 2011-10-27 21:39:33.958143
INFO:root:DELETE event : /tmp/adjtime 2011-10-27 21:39:33.962497
INFO:root:DELETE event : /tmp/aliases 2011-10-27 21:39:33.978506
INFO:root:DELETE event : /tmp/bash.bashrc 2011-10-27 21:39:33.980834
INFO:root:DELETE event : /tmp/bindresvport.blacklist 2011-10-27 21:39:33.997176
INFO:root:DELETE event : /tmp/environment 2011-10-27 21:39:33.997683
INFO:root:DELETE event : /tmp/fstab 2011-10-27 21:39:33.998110
INFO:root:DELETE event : /tmp/ipt.err 2011-10-27 21:39:33.998532
INFO:root:DELETE event : /tmp/ipt.out 2011-10-27 21:39:34.000360
INFO:root:DELETE event : /tmp/krb5.conf 2011-10-27 21:39:34.000816
INFO:root:DELETE event : /tmp/odbc.ini 2011-10-27 21:39:34.002217
INFO:root:DELETE event : /tmp/odbcinst.ini 2011-10-27 21:39:34.002675
INFO:root:DELETE event : /tmp/timezone 2011-10-27 21:39:34.003110
INFO:root:DELETE event : /tmp/ucf.conf 2011-10-27 21:39:34.003538
INFO:root:DELETE event : /tmp/warnquota.conf 2011-10-27 21:39:34.018152
INFO:root:DELETE event : /tmp/wgetrc 2011-10-27 21:39:34.018641
INFO:root:DELETE event : /tmp/xinetd.conf 2011-10-27 21:39:34.041880
INFO:root:CREATE event : /tmp/aa 2011-10-27 21:39:40.639965
INFO:root:OPEN event : /tmp/aa 2011-10-27 21:39:40.640914
INFO:root:IN_ATTRIB event : /tmp/aa 2011-10-27 21:39:40.647835
INFO:root:CLOSE_WRITE event : /tmp/aa 2011-10-27 21:39:40.652158
INFO:root:CREATE event : /tmp/bb 2011-10-27 21:39:43.064526
INFO:root:OPEN event : /tmp/bb 2011-10-27 21:39:43.070849
INFO:root:IN_ATTRIB event : /tmp/bb 2011-10-27 21:39:43.071329
INFO:root:CLOSE_WRITE event : /tmp/bb 2011-10-27 21:39:43.071762
INFO:root:CREATE event : /tmp/cc 2011-10-27 21:39:47.046752
INFO:root:OPEN event : /tmp/cc 2011-10-27 21:39:47.051537
INFO:root:IN_ATTRIB event : /tmp/cc 2011-10-27 21:39:47.056211
INFO:root:CLOSE_WRITE event : /tmp/cc 2011-10-27 21:39:47.057490
[root@centos6 tmp]# http://www.pyshell.com/index.php/archives/477
Python 的 pyinotify 模块 监控文件夹和文件的变动的更多相关文章
- python批量处理文件夹中文件的问题
用os模块读取文件夹中文件 原来的代码: import osfrom scipy.misc import imread filenames=os.listdir(r'./unprocess')for ...
- Python 文件夹及文件操作
import os import os.path from shutil import copy def copyfile(src, dst): count = 1 for filename in o ...
- Python学习(九)IO 编程 —— 文件夹及文件操作
Python 文件夹及文件操作 我们经常会与文件和目录打交道,对于这些操作,python可以使用 os 及 shutill 模块,其中包含了很多操作文件和目录的函数. os 可以执行简单的文件夹及文件 ...
- python文件夹遍历,文件操作,获取文件修改创建时间
在Python中,文件操作主要来自os模块,主要方法如下: os.listdir(dirname):列出dirname下的目录和文件os.getcwd():获得当前工作目录os.curdir:返回当前 ...
- python调用另一个文件中的代码,pycharm环境下:同文件夹下文件(.py)之间的调用,出现红线问题
如何调用另一个python文件中的代码无论我们选择用何种语言进行程序设计时,都不可能只有一个文件(除了“hello world”),通常情况下,我们都需要在一个文件中调用另外一个文件的函数呀数据等等, ...
- 使用FileSystemWatcher监控文件夹及文件
引言 这一周主要精力集中学习一个同事开发的本地文件搜索项目上,其中客户端添加共享文件时主要是使用FileSystemWatcher 监控文件,并在各种事件发生时向服务器发送消息. 解决方法 FileS ...
- python 实现彻底删除文件夹和文件夹下的文件
python 中有很多内置库可以帮忙用来删除文件夹和文件,当面对要删除多个非空文件夹,并且目录层次大于3层以上时,仅使用一种内置方法是无法达到彻底删除文件夹和文件的效果的,比较low的方式是多次调用直 ...
- 有关文件夹与文件的查找,删除等功能 在 os 模块中实现
最近在写的程序频繁地与文件操作打交道,这块比较弱,还好在百度上找到一篇不错的文章,这是原文传送门,我对原文稍做了些改动. 有关文件夹与文件的查找,删除等功能 在 os 模块中实现.使用时需先导入这个模 ...
- Python模糊查询本地文件夹去除文件后缀(7行代码)
Python模糊查询本地文件夹去除文件后缀 import os,re def fuzzy_search(path): word= input('请输入要查询的内容:') for filename in ...
随机推荐
- Command: sl (Steam Locomotive)
You might be aware of command 'ls' the list command and use it frequently to view the contents of a ...
- 安装rabbitMQ delayed-messaged
由于一些原因,消息需要延迟发送给消费者,可以用delayed-messaged插件 在 /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.5/plugins目录下 # ...
- linux服务器下安装nodejs
http://www.cnblogs.com/kevinchou/p/5405540.html
- django_cms安装技巧
首先python的版本要高一些,否则安装django-cms会报错 安装cmsinstaller不能够正常下载 利用virtualenv进行安装配置 注意中文的配置 djangocms配置中文 dja ...
- struct结构体(剽窃别人的)
结构是使用 struct 关键字定义的,与类相似,都表示可以包含数据成员和函数成员的数据结构. 一般情况下,我们很少使用结构,而且很多人也并不建议使用结构,但作为.NET Framework 一般型別 ...
- 关于java序列化中的一个细节
java序列化机制的可以参考很多资料了,最近在看的时候发现了一些问题. 1. 默认的序列化机制,很多书里讲到序列化类只序列化类名,实例变量,不会实例化类变量(static)和瞬态变量(transien ...
- 转!!负载均衡器技术Nginx和F5的优缺点对比
对于数据流量过大的网络中,往往单一设备无法承担,需要多台设备进行数据分流,而负载均衡器就是用来将数据分流到多台设备的一个转发器. 目前有许多不同的负载均衡技术用以满足不同的应用需求,如软/硬件负载均衡 ...
- OpenCV学习笔记(一)——OpenCV安装
1.无脑安装以下安装文件 cn_visual_studio_2010_ultimate_x86_dvd_532347.iso 2.测试Hello OpenCV 文件→新建→项目 win32应用程序→下 ...
- elk
http://467754239.blog.51cto.com/4878013/1700828/
- SQL Server 2012 联机丛书安装
已连接 Internet 的计算机: 单击"管理帮助设置"启动帮助库管理器. 在 Windows 7 中,依次选择"开始"."所有程序".& ...