当我指定的文件被修改后,我想调用我的chrome或firefox浏览器.修改后,我该如何观看"该文件以执行某些操作?
I would like to invoke my chrome or firefox browser when a file that I specify is modified. How could I "watch" that file to do something when it gets modified?
以编程方式看来,这些步骤是..基本上每秒设置一个永不结束的间隔,并缓存初始修改日期,然后在更改时调用X每秒比较一次该日期.
Programmatically it seems the steps are.. basically set a never ending interval every second or so and cache the initial modification date, then compare the date every second, when it changes invoke X.
推荐答案如前所述,您可以使用 pyinotify :
As noted, you can use pyinotify:
例如:
import webbrowser import pyinotify class ModHandler(pyinotify.ProcessEvent): # evt has useful properties, including pathname def process_IN_CLOSE_WRITE(self, evt): webbrowser.open(URL) handler = ModHandler() wm = pyinotify.WatchManager() notifier = pyinotify.Notifier(wm, handler) wdd = wm.add_watch(FILE, pyinotify.IN_CLOSE_WRITE) notifier.loop()这比轮询更有效.内核会告诉您何时进行操作,而无需您不断询问.
This is more efficient than polling. The kernel tells you when it does the operation, without you having to constantly ask.
更多推荐
我该如何“观看"修改/更改文件?
发布评论