当文件被删除时检测Python中损坏的流

发布于 2024-11-06 10:17:18 字数 344 浏览 0 评论 0原文

我的问题是,当日志轮换时,Python 程序的日志记录会停止。 我已经追踪到了流本身。我没有看到任何方法来判断流是否从 python 中断。文件删除后,它仍然接受写入,没有任何问题。

import os

FILE = 'testing.txt'

fs = open(FILE, 'a')
fs.write('word')
os.remove(FILE)
fs.write('Nothing....') # Nothing breaks
print(fs.errors) # No errors

那么,如何确定文件流是否仍然有效呢? 检查文件是否存在也无济于事,因为无论流是否仍然有效,文件都将始终存在。

My problem is that logging stops for a python program when the log is rotated.
I have tracked it down to the stream itself. I don't see any way to tell if the stream is broken from python. After the file is deleted it still accepts writes without any issue.

import os

FILE = 'testing.txt'

fs = open(FILE, 'a')
fs.write('word')
os.remove(FILE)
fs.write('Nothing....') # Nothing breaks
print(fs.errors) # No errors

So, how can I find out if the file stream is still valid?
And checking to see if the file exists will not help since the file will always exist regardless of whether or not the stream is still valid.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑红尘 2024-11-13 10:17:18

经过更多检查,我找到了解决方案。这是操作系统特定的问题。当文件在 Linux(或 Macintosh)中被删除时,它只是取消链接。 (我不知道这一点)
因此,如果您在计算机上运行 lsof,它仍然会将该文件显示为打开状态。

[user@machine]$ lsof | grep --color -i "testing.txt"
python26  26495    user    8w      REG               8,33     23474     671920 /home/user/temp/testing.txt (deleted)

解决方案是在 python 中统计流。

stat = os.fstat(fs.fileno())

这将为您提供其拥有的链接数量。

if stat.st_nlink < 1:
    #has been deleted

就这样吧。现在您知道是否应该重新加载它。希望这对其他人有帮助。

Upon much more inspection, I found the solution. It is an OS specific problem. When the file is deleted in Linux (or Macintosh) it just unlinks it. (I was not aware of this)
So if you run lsof on the machine, it still shows the file as open.

[user@machine]$ lsof | grep --color -i "testing.txt"
python26  26495    user    8w      REG               8,33     23474     671920 /home/user/temp/testing.txt (deleted)

The solution is to stat the stream in python.

stat = os.fstat(fs.fileno())

Which will give you the number of links it has.

if stat.st_nlink < 1:
    #has been deleted

And there you go. Now you know if you should reload it or not. Hopefully this helps someone else.

衣神在巴黎 2024-11-13 10:17:18

尝试异常处理

import os

FILE = 'testing.txt'

try:
    fs = open(FILE, 'a')
    fs.write('word')
    os.remove(FILE)
    fs.write('Nothing....') # Nothing breaks
except Exception, e:
    print "Error:", e
print(fs.errors) # No errors

Try Exception handling:

import os

FILE = 'testing.txt'

try:
    fs = open(FILE, 'a')
    fs.write('word')
    os.remove(FILE)
    fs.write('Nothing....') # Nothing breaks
except Exception, e:
    print "Error:", e
print(fs.errors) # No errors
仲春光 2024-11-13 10:17:18

如果您需要更多的智能而不仅仅是 try: except: 子句,则可以使用 ionotify 的 python 绑定。但我认为它只与 Linux 有关(我不确定你的平台)

There are python bindings for ionotify if you need more intelligence than just an try: except: clause. But I think its only pertinent to Linux (im not sure of your platform)

幸福%小乖 2024-11-13 10:17:18

我发现的另一个解决方案是将“copytruncate”标志添加到 logrotate 配置中。
有关更多信息,请参阅“man logrotate”。

Another solution I found is to add the "copytruncate" flag into the logrotate config.
See "man logrotate" for more info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文