linux下用python脚本监控目录变化代码分享

#!/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

Posted in 未分类