如何使用 python 修改文件时执行一些代码?
我想在每次用新数据写入文件(修改)时执行一个函数,并且我使用的是 Python。
我该怎么做呢?
I want to execute one function each time a file gets written with new data (gets modified) and I'm using Python.
How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想使用 Python 监视文件系统中的更改,请参阅 这篇文章提供了一些库的指针,可以帮助您在不同的平台上实现这一目标。
适用于不同平台的可用库的简短摘要:
请记住,您的程序需要运行才能处理文件系统事件。
If you want to monitor for changes in the file system using Python, see this article for pointers to libraries which can help you achieve this on different platforms.
Short summary of available libraries for different platforms:
Remember that your program needs to be running to handle file system events.
我假设不一定是您的 Python 程序正在修改该文件。我不知道是否有独立于操作系统的方法。在 Linux 上,您可以使用 pyinotify。
I'm assuming that it's not necessarily your Python program that's modifying the file. I don't know if there's an OS-independent method. On Linux you can use pyinotify.