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

yipeiwu_com5年前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使用 __init__初始化操作简单示例

本文实例讲述了python使用 __init__初始化操作。分享给大家供大家参考,具体如下: # -*- coding:utf-8 -*- # !/usr/bin/python cl...

查看TensorFlow checkpoint文件中的变量名和对应值方法

实例如下所示: from tensorflow.python import pywrap_tensorflow checkpoint_path = os.path.join(mode...

Python实现的企业粉丝抽奖功能示例

Python实现的企业粉丝抽奖功能示例

本文实例讲述了Python实现的企业粉丝抽奖功能。分享给大家供大家参考,具体如下: 一 代码 def scode9(schoice): default_dir = r"lotte...

Python优化技巧之利用ctypes提高执行速度

首先给大家分享一个个人在使用python的ctypes调用c库的时候遇到的一个小坑 这次出问题的地方是一个C函数,返回值是malloc生成的字符串地址。平常使用也没问题,也用了有段时间,...

利用python脚本如何简化jar操作命令

前言 本篇和大家分享的是使用python简化对jar包操作命令,封装成简短关键字或词,达到操作简便的目的。最近在回顾和构思shell脚本工具,后面一些文章应该会分享shell内容,希望大...