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语言

Python 是 90 年代初由 Guido Van Rossum 创立的。它是当前最流行的程序语言之一。它那纯净的语法令我一见倾心,它简直就是可以运行的伪码。 请注意:本文以 Pyth...

python使用pdfminer解析pdf文件的方法示例

最近要做个从 pdf 文件中抽取文本内容的工具,大概查了一下 python 里可以使用 pdfminer 来实现。下面就看看怎样使用吧。 PDFMiner是一个可以从PDF文档中提取信息...

python 实现兔子生兔子示例

如下所示: # -*- coding: utf-8 -*- # 简述:话说有一对可爱的兔子,出生后的第三个月开始,每一月都会生一对小兔子。 # 当小兔子长到第三个月后,也会每个月再生...

python sys,os,time模块的使用(包括时间格式的各种转换)

sys模块 sys.argv: 实现从程序外部向程序传递参数。 位置参数argv[0]代表py文件本身,运行方法 python xx.py 参数1,参数2 。。 self = s...

Python psutil模块简单使用实例

安装很简单 复制代码 代码如下: pip install psutil 官网地址为: https://pythonhosted.org/psutil/ (文档上有详细的api) git...