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

yipeiwu_com4年前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编程二分法实现冒泡算法+快速排序,具体如下。 冒泡算法: #-*- coding: UTF-8 -*- #冒泡排序 def func(lt): if...

手写一个python迭代器过程详解

分析 我们都知道一个可迭代对象可以通过iter()可以返回一个迭代器。 如果想要一个对象称为可迭代对象,即可以使用for,那么必须实现__iter __()方法。 在一个类...

python的类方法和静态方法

本文实例讲述了python的类方法和静态方法。分享给大家供大家参考。具体分析如下: python没有和C++中static关键字,它的静态方法是怎样的呢?还有其它语言中少有的类方法又是神...

Python学习笔记整理3之输入输出、python eval函数

1. python中的变量: python中的变量声明不需要像C++、Java那样指定变量数据类型(int、float等),因为python会自动地根据赋给变量的值确定其类型。如 rad...

在ironpython中利用装饰器执行SQL操作的例子

比较喜欢python的装饰器, 试了下一种用法,通过装饰器来传递sql,并执行返回结果 这个应用应该比较少 为了方便起见,直接使用了ironpython, 连接的mssql server...