python监控文件目录变化多实例代码

发布时间:2020-10-18编辑:脚本学堂
python监控文件或目录变化,多是使用pyinotify模块,这里分享几个例子,学习下pyinotify模块的用法,深入理解python监控文件变化的原理与实现方法。

例1,pyinotify模块监控文件变化。
 

复制代码 代码示例:

#!/usr/bin/env python
#coding=utf-8

import os
from pyinotify import WatchManager, Notifier, ProcessEvent, IN_DELETE, IN_CREATE,IN_MODIFY
wm = WatchManager()
mask = IN_DELETE | IN_CREATE |IN_MODIFY   # watched events

class PFilePath(ProcessEvent):
def process_IN_CREATE(self, event):
print   "Create file: %s " %   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)

if __name__ == "__main__":

notifier = Notifier(wm, PFilePath())
wdd = wm.add_watch('.', mask, rec=True)

while True:
try :
notifier.process_events()
if notifier.check_events():
notifier.read_events()
except KeyboardInterrupt:
notifier.stop()
break
 

  python 监视文件目录

摘要 python 下使用watchdog 来监视文件目录,触发相应的操作。
python

该例子为用watchdog来监视新文件,当新文件来时候,调用相应的解析脚本,进行解析入库。

目录:
-scripts
--脚本1.py
--脚本2.py

-tmp
--已处理的文件1,
--已处理的文件2

config.py
watchdog.py

watchdog.py 文件,该业务只监听新文件创建的事件:
 

复制代码 代码示例:
#!/usr/bin/env python
# coding=utf8
import sys
import time
import logging
import imp
import re
import scripts.CONFIG
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
from watchdog.events import FileSystemEventHandler
 
class CreatedEventHandler(FileSystemEventHandler):
 
def __init__(self):
FileSystemEventHandler.__init__(self)
 
 
def on_created(handler,event):
file_name = event.src_path[2:]
print '--'+file_name
 
moduleName = ''
for key in parse_map.keys():
if(re.match(key,file_name)):
moduleName = parse_map[key]
break
if(moduleName != ''):
try:
#动态加载相应的module
parseModule =  imp.load_module(moduleName,*imp.find_module(moduleName,['./scripts/']))
print '  load module: ' + moduleName
parseModule.parse(file_name)
except Exception,e:
print e
 
#正则匹配,将文件match到相应的解析脚本上
parse_map={
'^test.xlsx$':'test',
'^emt_finance.*.xlsx':'emt_finance'
}
 
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s',datefmt='%Y-%m-%d %H:%M:%S')
path = sys.argv[1] if len(sys.argv) > 1 else '.'
event_handler = CreatedEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=False)
observer.start()
print 'Watching...'
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()

解析脚本test.py
 

复制代码 代码示例:
#!/usr/bin/env python
# FileName: test.py
# a simple test code
import xlrd
import mysqldb/ target=_blank class=infotextkey>MySQLdb
import datetime
import os
import stat
import shutil
 
def parse(file):
values = []
if(file.split('.')[-1] != 'xlsx'):
print '---skip' + file
return
try:
data = xlrd.open_workbook(file)
table = data.sheets()[0]
for i in range(1,table.nrows):
row = table.row_values(i)
#excel date is the days from 1899/12/30
row[0] = datetime.date(1899,12,30) + datetime.timedelta(row[0])
values.append(row)
except Exception,e:
print e
 
#print values
try:
conn = MySQLdb.connect(config.mysql_host,config.mysql_user,config.mysql_passwd,'test',config.mysql_port)
cur = conn.cursor()
#values[0][5]=4
for v in values:
count = cur.execute('replace into testtable values(%s,%s,%s,%s,%s,%s)',v)
 
conn.commit()
print '  parse complete.'
'''
results=cur.fetchmany(5)
for r in results:
print r
'''
cur.close()
conn.close()
 
#os.remove('tmp/test.pyc')
 
if(os.path.exists('tmp/'+file)):
os.chmod('tmp/'+file,stat.S_IWRITE)#去掉只读属性
os.remove('tmp/'+file) #删除它
 
shutil.move(file,'tmp/')
print '  move filt to temp: ' + file
print '  success!'
 
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
  
if __name__ == '__main__':
exec "import CONFIG as config"
print '=='*10
#os.remove('../tmp/test.xlsx')
#shutil.move('../test.xlsx','tmp/')
#parse('../test.xlsx')
else:
exec "import scripts.CONFIG as config"

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
# 脚本学堂 www.jb200.com

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
aabash.bashrc ipt.errodbcinst.iniwgetrc
adduser.conf  bindresvport.blacklist  ipt.outtimezonexinetd.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