[英]how to make pyinotify to run a program on any modification over a file?
我必須觀察給出的任何輸入或對文件中的當前內容所做的任何更改,在進行任何修改時我需要運行位於同一文件夾中的python程序。
我盡力了解,但我無法取得任何好成績。 如果有人能幫我解決這個問題,那將會有很大的幫助。
謝謝.. :)
import pyinotify,subprocess
def onChange(ev):
cmd = ['/bin/echo', 'File', ev.pathname, 'changed']
subprocess.Popen(cmd).communicate()
wm = pyinotify.WatchManager()
wm.add_watch('file.watched', pyinotify.IN_MODIFY, onChange)
notifier = pyinotify.Notifier(wm)
notifier.loop()
顯然,將cmd
替換為您要執行的命令,並將file.watched
替換為您要監視的文件。
來自http://schettino72.wordpress.com/tag/inotify/
我正在努力為doit添加一些inotify優點。 為此,我希望每次修改文件時都會收到一個和一個通知。 Inotify讓人很難看到文件系統,而Pyinotify提供了一個python接口。 但是使用它並不像我預期的那樣直截了當。 問題是編輯器以自己的方式操縱文件......
當我使用“echo”時它工作正常。 但是當我嘗試使用Emacs時,我收到了3個通知。 使用VIM甚至最糟糕,我沒有收到通知和錯誤消息!
獲得phihag的優秀例子
wm.add_watch('file.watched', pyinotify.IN_MODIFY, onChange)
可能:
wm.add_watch('file.watched', pyinotify.IN_CLOSE_WRITE, onChange)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.