当前位置: 移动技术网 > IT编程>脚本编程>Python > Python pyinotify模块实现对文档的实时监控功能方法

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

2018年11月09日  | 移动技术网IT编程  | 我要评论

qq默认表情改版包,珍妮弗沃弗和玛雅遗迹攻略,雷锋做好事的故事

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模块实现对文档的实时监控功能方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网