Python中使用Inotify监控文件实例,
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)
相关内容
- Python中的MongoDB基本操作:连接、查询实例,pythonmongo
- Python实现获取某天是某个月中的第几周,python第几周
- Python脚本自动更新hosts实现免翻墙访问google,
- Python脚本实现下载合并SAE日志,python脚本sae日志
- Python中使用HTMLParser解析html实例,pythonhtmlparser
- Python中装饰器的一个妙用,Python装饰器妙用
- Python Web框架Flask中使用新浪SAE云存储实例,flasksae
- Python Web框架Flask中使用百度云存储BCS实例,flaskbcs
- Python Web框架Flask中使用七牛云存储实例,pythonflask
- Python中解析JSON并同时进行自定义编码处理实例,pytho
评论关闭