前提:
自动化测试过程中生成的测试报告想发送给前端展示,但是脚本执行结束比较难判断,通过监听测试报告文件的生成来触发,而python中比较简单的库就是watchdog
实例:
#! /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 = '/Users/xxx/xxx/' # 监控目录
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)
def on_created(self, event):
if not event.is_directory: # 文件改变都会触发文件夹变化
file_path = event.src_path
file_name = file_path.replace(WATCH_PATH, "")
file_name = file_name.replace(".html", "")
print("文件改变: %s " % file_path)
print("文件改变: %s " % file_name)
if __name__ == "__main__":
event_handler = FileMonitorHandler()
observer = Observer()
observer.schedule(event_handler, path=WATCH_PATH, recursive=True) # recursive递归的
observer.start()
observer.join()
解析
事件类型(增删改查监控):
on_moved
on_modified
on_created
on_deleted
用于监控:
observer.schedule
网友评论