Python pyinotify模块实现对文档的实时监控功能方法

yipeiwu_com6年前Python基础

0x01 安装pyinotify

>>> pip install pyinotify
>>> import pyinotify

0x02 实现对文档的试试监控功能

这个功能类似与Ubuntu里的rail -f功能,在对目标文件进行修改时,脚本可以实时监控并将新的修改打印出来。

import pyinotify
import time
import os

class ProcessTransientFile(pyinotify.ProcessEvent):
  def process_IN_MODIFY(self, event):
    line = file.readline()
    if line:
      print line, # already has newline

filename = './test.txt'
file = open(filename,'r')
#Find the size of the file and move to the end
st_results = os.stat(filename)
st_size = st_results[6]
file.seek(st_size)

wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm)
wm.watch_transient_file(filename, pyinotify.IN_MODIFY, ProcessTransientFile)

notifier.loop()

以上这篇Python pyinotify模块实现对文档的实时监控功能方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python交互界面的退出方法

1.在终端输入python,进入之后退出: quit() 或者 exit() 2,进入idle shell下的退出 关闭: quit() 或者 exit() 或...

Python 类的魔法属性用法实例分析

本文实例讲述了Python 类的魔法属性用法。分享给大家供大家参考,具体如下: 魔法属性 无论人或事物往往都有不按套路出牌的情况,Python的类属性也是如此,存在着一些具有特殊含义的属...

Python中模拟enum枚举类型的5种方法分享

以下几种方法来模拟enum:(感觉方法一简单实用) 复制代码 代码如下: # way1 class Directions:     up = 0  ...

PYTHON基础-时间日期处理小结

步骤: 1. 掌握几种对象及其关系 2. 了解每类对象的基本操作方法 3. 通过转化关系转化 涉及对象 1. datetime >>> import datetim...

selenium+python自动化测试环境搭建步骤

selenium+python自动化测试环境搭建步骤

相对于自动化测试工具QTP来说,selenium小巧、免费,而且兼容Google、FireFox、IE多种浏览器,越来越多的人开始使用selenium进行自动化测试。 我是使用的pyth...