0x01 安裝pyinotify
>>> pip install pyinotify>>> import pyinotify
0x02 實(shí)現(xiàn)對(duì)文檔的試試監(jiān)控功能
這個(gè)功能類似與Ubuntu里的rail -f功能,在對(duì)目標(biāo)文件進(jìn)行修改時(shí),腳本可以實(shí)時(shí)監(jiān)控并將新的修改打印出來。
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模塊實(shí)現(xiàn)對(duì)文檔的實(shí)時(shí)監(jiān)控功能方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選