我该如何“观看"修改/更改文件? [英] How can I "watch" a file for modification / change?

查看:113
本文介绍了我该如何“观看"修改/更改文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我指定的文件被修改后,我想调用我的chromefirefox浏览器.修改后,我该如何观看"该文件以执行某些操作?

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.

推荐答案

如前所述,您可以使用

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.

这篇关于我该如何“观看"修改/更改文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆