0x01 安裝pyinotify
>>> pip install pyinotify>>> import pyinotify
0x02 實現對文檔的試試監控功能
這個功能類似與Ubuntu里的rail -f功能,在對目標文件進行修改時,腳本可以實時監控并將新的修改打印出來。
import pyinotifyimport timeimport osclass ProcessTransientFile(pyinotify.ProcessEvent): def process_IN_MODIFY(self, event): line = file.readline() if line: print line, # already has newlinefilename = './test.txt'file = open(filename,'r')#Find the size of the file and move to the endst_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模塊實現對文檔的實時監控功能方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。
新聞熱點
疑難解答