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 Image模块基本图像处理操作小结

本文实例讲述了Python Image模块基本图像处理操作。分享给大家供大家参考,具体如下: Python 里面最常用的图像操作库是Image library(PIL),功能上,虽然还不...

详解python项目实战:模拟登陆CSDN

详解python项目实战:模拟登陆CSDN

前言 今天为大家介绍一个利用Python模拟登陆CSDN的案例,虽然看起来很鸡肋,有时候确会有大用处,在这里就当做是一个案例练习吧,提高自己的代码水平,也了解Python如何做到模拟登陆...

python读取几个G的csv文件方法

如下所示: import pandas as pd file = pd.read_csv('file.csv',iterator=True) while True: chunk...

python使用selenium实现批量文件下载

python使用selenium实现批量文件下载

背景 实现需求:批量下载联想某型号的全部驱动程序。 一般在做网络爬虫的时候,都是保存网页信息为主,或者下载单个文件。当涉及到多文件批量下载的时候,由于下载所需时间不定,下载的文件名不定,...

Python实现端口复用实例代码

本文介绍Python实现端口复用实例如下所示: #coding=utf-8 import socket import sys import select import threadi...