Python中使用Inotify监控文件实例

时间:2021-05-22

Inotify地址:访问

# -*- coding:utf-8 -*-import osimport pyinotifyfrom 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() breakif __name__ == "__main__": auto_compile(WATCH_PATH)

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章