• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python中使用Inotify监控文件实例

python 搞代码 4年前 (2022-01-09) 11次浏览 已收录 0个评论

Inotify地址:访问

# -*- coding:utf<b style="color:transparent">本文来源gao@!dai!ma.com搞$$代^@码网*</b>-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)

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python中使用Inotify监控文件实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址