业务场景:
最近公司要开发云虚拟机文件操作平台,其中重要的一步就是记录出用户进入该windows虚拟机,对文件的操作行为。
查了一上午资料,在python中文件监控主要有两个库,一个是pyinotify ( https://github.com/seb-m/pyinotify/wiki ),一个是watchdog(http://pythonhosted.org/watchdog/)。pyinotify依赖于Linux平台的inotify,后者则对不同平台的的事件都进行了封装。因为我主要用于Windows平台,所以下面着重介绍python使用watchdog对文件及文件夹变化实时监控,具体细节,以后对在更新文章。下面是一个简单的demo,修改路径就可以跑起来。
安装watchdog:
pip install watchdog
代码实现
from watchdog.observers import Observer
from watchdog.events import *
import time
class FileEventHandler(FileSystemEventHandler):
def __init__(self):
FileSystemEventHandler.__init__(self)
def on_m