python中使用inotify监控文件实例

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)

Posted in 未分类

发表评论