欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python文件监听工具pyinotify与watchdog实例

程序员文章站 2023-08-26 18:30:30
pyinotify库 支持的监控事件 @cvar in_access: file was accessed. @type in_access: int @c...

pyinotify库

支持的监控事件

@cvar in_access: file was accessed.
@type in_access: int
@cvar in_modify: file was modified.
@type in_modify: int
@cvar in_attrib: metadata changed.
@type in_attrib: int
@cvar in_close_write: writtable file was closed.
@type in_close_write: int
@cvar in_close_nowrite: unwrittable file closed.
@type in_close_nowrite: int
@cvar in_open: file was opened.
@type in_open: int
@cvar in_moved_from: file was moved from x.
@type in_moved_from: int
@cvar in_moved_to: file was moved to y.
@type in_moved_to: int
@cvar in_create: subfile was created.
@type in_create: int
@cvar in_delete: subfile was deleted.
@type in_delete: int
@cvar in_delete_self: self (watched item itself) was deleted.
@type in_delete_self: int
@cvar in_move_self: self (watched item itself) was moved.
@type in_move_self: int
@cvar in_unmount: backing fs was unmounted.
@type in_unmount: int
@cvar in_q_overflow: event queued overflowed.
@type in_q_overflow: int
@cvar in_ignored: file was ignored.
@type in_ignored: int
@cvar in_onlydir: only watch the path if it is a directory (new
         in kernel 2.6.15).
@type in_onlydir: int
@cvar in_dont_follow: don't follow a symlink (new in kernel 2.6.15).
           in_onlydir we can make sure that we don't watch
           the target of symlinks.
@type in_dont_follow: int
@cvar in_excl_unlink: events are not generated for children after they
           have been unlinked from the watched directory.
           (new in kernel 2.6.36).
@type in_excl_unlink: int
@cvar in_mask_add: add to the mask of an already existing watch (new
          in kernel 2.6.14).
@type in_mask_add: int
@cvar in_isdir: event occurred against dir.
@type in_isdir: int
@cvar in_oneshot: only send event once.
@type in_oneshot: int
@cvar all_events: alias for considering all of the events.
@type all_events: int

python 3.6的demo

import sys
import os
import pyinotify
watch_path = '/home/lp/ftp' # 监控目录
if not watch_path:
  print("the watch_path setting must be set.")
  sys.exit()
else:
  if os.path.exists(watch_path):
    print('found watch path: path=%s.' % (watch_path))
  else:
    print('the watch path not exists, watching stop now: path=%s.' % (watch_path))
    sys.exit()
# 事件回调函数
class oniohandler(pyinotify.processevent):
  # 重写文件写入完成函数
  def process_in_close_write(self, event):
    # logging.info("create file: %s " % os.path.join(event.path, event.name))
    # 处理成小图片,然后发送给grpc服务器或者发给kafka
    file_path = os.path.join(event.path, event.name)
    print('文件完成写入',file_path)
  # 重写文件删除函数
  def process_in_delete(self, event):
    print("文件删除: %s " % os.path.join(event.path, event.name))
  # 重写文件改变函数
  def process_in_modify(self, event):
    print("文件改变: %s " % os.path.join(event.path, event.name))
  # 重写文件创建函数
  def process_in_create(self, event):
    print("文件创建: %s " % os.path.join(event.path, event.name))
def auto_compile(path='.'):
  wm = pyinotify.watchmanager()
  # mask = pyinotify.eventscodes.all_flags.get('in_create', 0)
  # mask = pyinotify.eventscodes.flag_collections['op_flags']['in_create']               # 监控内容,只监听文件被完成写入
  mask = pyinotify.in_create | pyinotify.in_close_write
  notifier = pyinotify.threadednotifier(wm, oniohandler())  # 回调函数
  notifier.start()
  wm.add_watch(path, mask, rec=true, auto_add=true)
  print('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)
  print('monitor close')

watchdog库

支持的监控事件

event_type_modified: self.on_modified,
event_type_moved: self.on_moved,
event_type_created: self.on_created,
event_type_deleted: self.on_deleted,

需要注意的是,文件改变,也会触发文件夹的改变

python3.6的demo

#! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import asyncio
import base64
import logging
import os
import shutil
import sys
from datetime import datetime
from watchdog.events import filesystemeventhandler
from watchdog.observers import observer
watch_path = '/home/lp/ftp' # 监控目录
class filemonitorhandler(filesystemeventhandler):
 def __init__(self, **kwargs):
  super(filemonitorhandler, self).__init__(**kwargs)
  # 监控目录 目录下面以device_id为目录存放各自的图片
  self._watch_path = watch_path
 # 重写文件改变函数,文件改变都会触发文件夹变化
 def on_modified(self, event):
  if not event.is_directory: # 文件改变都会触发文件夹变化
   file_path = event.src_path
   print("文件改变: %s " % file_path)
if __name__ == "__main__":
 event_handler = filemonitorhandler()
 observer = observer()
 observer.schedule(event_handler, path=watch_path, recursive=true) # recursive递归的
 observer.start()
 observer.join()

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接