Python pyinotify module realizes real time monitoring of documents

  • 2020-12-16 05:59:34
  • OfStack

0 x01 pyinotify installation


>>> pip install pyinotify
>>> import pyinotify

0x02 to achieve the document trial monitoring function

Similar to the ES10en-ES11en feature in Ubuntu, the script can monitor and print out changes to the target file in real time as they are made.


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()

Related articles: