我该如何“观看"修改/更改文件?

编程入门 行业动态 更新时间:2024-10-27 18:24:32
本文介绍了我该如何“观看"修改/更改文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我指定的文件被修改后,我想调用我的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.

更多推荐

我该如何“观看"修改/更改文件?

本文发布于:2023-11-06 17:36:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564348.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我该   文件   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!