如何使我的Python程序检查并等待可访问TXT文件?

发布于 2025-01-20 03:00:39 字数 354 浏览 3 评论 0原文

问题
我有第三方软件可以生成文本文件并在x秒后附加一条新行。

我的程序从文本文件中读取数据点,并以Funcanimation的形式显示为Matplotlib中的图。但是,有时它会减慢,因为该软件和Python显然尝试在定时重叠时同时访问文本文件。

我的目标
我希望在任何时间点开始启动时python代码,以检查是否正在使用文本文件,如果是,请等到它不再使用。这意味着我希望它仅在软件实际上未使用文件时尝试访问该文件。

感谢您的帮助! (我认为没有必要在此处介绍代码示例,因为这是一个更普遍的问题,而不是我的代码中的一个特定问题。当然,如果明确希望,我会这样做!)

Problem
I have third-party software that generates a text file and appends a new line to it after x seconds.

My program reads the data points live from the text file and displays values as a plot in Matplotlib with FuncAnimation. However, sometimes it slows down because the software and Python obviously try to access the text file simultaneously when their timing overlaps.

My Goal
I want the Python code, when started at any point in time, to check if the text file is currently being used and if so, wait until it is not anymore. That means I want it to only try to access the file when it is actually not used by the software.

I appreciate your help!
(I do not think it is necessary to present a code example here because this is a more general question and not a specific problem in my code. Of course, I will do so if explicitly wished for!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文