pyinotify使用总结,一款能够监控你文件夹打开,修改,删除,重命名的开源工具...

本文介绍了一个强大的Python库Pyinotify,它能够监控文件的各种状态变化,包括但不限于文件被访问、元数据改变、文件关闭等。此外还提供了详细的使用案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自己调研了很多软件,只有这一个能够实现监控文件打开的状态,很强大,提供的example也很详细

 

github主页:https://github.com/seb-m/pyinotify/wiki/Events-types

 

可以监控到这些状态

  • IN_ACCESS: a file was accessed
  • IN_ATTRIB: a metadata changed
  • IN_CLOSE_NOWRITE: an unwritable file was closed
  • IN_CLOSE_WRITE: a writable file was closed
  • IN_CREATE: a file/directory was created in watched directory
  • IN_DELETE: a file/directory was deleted in watched directory
  • IN_DELETE_SELF: a watched item itself was deleted
  • IN_DONT_FOLLOW: don't follow a symlink (since kernel 2.6.15)
  • IN_IGNORED: raised when a watch is removed. Probably useless for you, prefer using instead relying on IN_DELETE*
  • IN_ISDIR: always associated with an event triggered on a directory. The Event structure automatically provides this information via attribute event.dir
  • IN_MASK_ADD: to update a mask without overwriting the previous value (since kernel 2.6.14). Useful when updating a watch
  • IN_MODIFY: a file was modified
  • IN_MOVE_SELF: a watched item was moved, currently its full pathname destination can only be known if its source and destination directories were both watched. Otherwise, the file is still being watched but you cannot rely anymore on the given path attribute event.path
  • IN_MOVED_FROM: a file/directory in a watched directory was moved from another specified watched directory. Can trace the full move of an item when IN_MOVED_TO is available too, in this case if the moved item is itself watched, its path will be updated (see IN_MOVE_SELF)
  • IN_MOVED_TO: a file/directory was moved to another specified watched directory (see IN_MOVE_FROM)
  • IN_ONLYDIR: only watch the path if it is a directory (since kernel 2.6.15). Usable when calling watch_manager.add_watch()
  • IN_OPEN: a file was opened.
  • IN_Q_OVERFLOW: the event queue overflown. This event is not associated with any watch descriptor
  • IN_UNMOUNT: when backing fs was unmounted. Notified to each watch of this fs 


  •  

 

利用看门狗(Watchdog)监控图片文件夹并实时检测新图片的过程可以分为以下几个步骤: 1. **设置看门狗**:首先,你需要在你的Python脚本中引入看门狗库(如`pyinotify`),这是一个用于监视文件系统事件的模块。当新的图片文件被创建到指定目录时,它会触发相应的回调函数。 ```python import pyinotify # 初始化看门狗对象 wm = pyinotify.WatchManager() mask = pyinotify.IN_CREATE | pyinotify.IN_MOVED_TO # 监听文件创建和移动事件 notifier = pyinotify.Notifier(wm) handler = MyHandler() # 自定义处理图片文件的类实例 wm.add_watch('your_image_folder', mask, proc_fun=handler.process_new_image) # 监控特定文件夹 ``` 2. **编写处理函数**:在`MyHandler`类中,定义`process_new_image`方法,这个方法会在接收到新的图片文件事件时被调用。在这个方法里,你可以读取新文件并运行YOLOv5的`detect.py`进行图像识别和标注。 ```python class MyHandler(pyinotify.ProcessEvent): def process_new_image(self, event): img_path = event.pathname # 获取新文件路径 img = cv2.imread(img_path) # 读取图片(假设cv2已导入) results = detect.detect(img) # 调用YOLOv5的detect方法 annotate_and_save(results, img_path) # 标注图片并保存 ``` 3. **YOLOv5检测和标注**:你需要确保`detect.py`脚本已经安装并在`yolov5`模型路径下可用。`annotate_and_save`是一个自定义函数,用于将检测结果应用到原始图片上,并将其保存到原位置或指定的新位置。 4. **启动监控**:最后,在你的程序开始时启动看门狗和监听器。 ```python if __name__ == "__main__": try: notifier.loop() # 开始监听文件系统变化 except KeyboardInterrupt: print("退出") notifier.stop() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值