Python中使用Inotify监控文件实例

yipeiwu_com5年前Python基础

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 字符串、列表、元组的截取与切片操作示例

本文实例讲述了Python 字符串、列表、元组的截取与切片操作。分享给大家供大家参考,具体如下: demo.py(字符串、列表、元组的截取): # 切片(截取) [开始索引:结束索引...

django使用xlwt导出excel文件实例代码

本文研究的主要是记录一下下导出的方法,并没有做什么REST处理和异常处理。 维护统一的style样式,可以使导出的数据更加美观。 def export_excel(request):...

Django应用程序入口WSGIHandler源码解析

前言 WSGI 有三个部分, 分别为服务器(server), 应用程序(application) 和中间件(middleware). 已经知道, 服务器方面会调用应用程序来处理请求, 在...

python SVM 线性分类模型的实现

python SVM 线性分类模型的实现

运行环境:win10 64位 py 3.6 pycharm 2018.1.1 导入对应的包和数据 import matplotlib.pyplot as plt import num...

用pyqt5 给按钮设置图标和css样式的方法

如下所示: 设置图标 self.pushButton.setIcon(QIcon("sure.png")) 设置css样式 self.pushButton.setStyleSh...