如何在 Python 中确定打开文件的大小?
我想确保有一个文件的大小不会超过 2 GB(因为它必须在使用 ext 2 的系统上运行)。考虑到我将在检查之间写入该文件,检查文件大小的好方法是什么?特别是,我是否需要担心尚未写入磁盘的缓冲的、未刷新的更改?
There's a file that I would like to make sure does not grow larger than 2 GB (as it must run on a system that uses ext 2). What's a good way to check a file's size bearing in mind that I will be writing to this file in between checks? In particular, do I need to worry about buffered, unflushed changes that haven't been written to disk yet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
也许不是你想要的,但无论如何我都会建议它。
或者,对于打开的文件,您可以使用 fstat 函数,该函数可以是用于打开的文件。它需要一个整数文件句柄,而不是文件对象,因此您必须在文件对象上使用 fileno 方法:
Perhaps not what you want, but I'll suggest it anyway.
Alternatively for an opened file you can use the fstat function, which can be used on an opened file. It takes an integer file handle, not a file object, so you have to use the fileno method on the file object:
os.fstat(file_obj.fileno()).st_size
应该可以解决问题。我认为它会返回写入的字节。如果您担心缓冲,您始终可以事先进行刷新。os.fstat(file_obj.fileno()).st_size
should do the trick. I think that it will return the bytes written. You can always do a flush before hand if you are concerned about buffering.虽然这是一个老问题,但我认为 Isak 有最简单的解决方案。以下是在 Python 中执行此操作的方法:
Though this is an old question, I think that Isak has the simplest solution. Here's how to do it in Python:
您可以从这样的开始:
然后您可以像这样使用它:
显然,如果您不是从头开始编写文件,则此实现不起作用,但您可以调整您的 __init__ 方法以处理初始数据。您可能还需要重写一些其他方法:例如
writelines
。无论编码如何,这都有效,因为字符串只是字节序列。
You could start with something like this:
Then you could use it like this:
Obviously, this implementation doesn't work if you aren't writing the file from scratch, but you could adapt your
__init__
method to handle initial data. You might also need to override some other methods:writelines
, for instance.This works regardless of encoding, as strings are just sequences of bytes.
我不熟悉 python,但是流对象(或打开文件时得到的任何内容)是否有一个包含流当前位置的属性?
类似于使用 ftell() C 函数得到的结果,或者 < .NET 中的 href="http://msdn.microsoft.com/en-us/library/system.io.stream.position.aspx" rel="nofollow noreferrer">Stream.Position。
显然,只有当您位于流的末尾(当前正在写入流的末尾)时,这才有效。
这种方法的好处是您不必关闭文件或担心未刷新的数据。
I'm not familiar with python, but doesn't the stream object (or whatever you get when opening a file) have a property that contains the current position of the stream?
Similar to what you get with the ftell() C function, or Stream.Position in .NET.
Obviously, this only works if you are positioned at the end of the stream, which you are if you are currently writing to it.
The benefit of this approach is that you don't have to close the file or worry about unflushed data.
或者,如果文件已打开:
这就是文件的字节数。
Or, if the file is already open:
That's how many bytes the file is.
最可靠的是创建一个包装类,它会在打开文件时检查文件的大小,跟踪写入和查找操作,根据这些操作计算当前大小并防止超出大小限制。
Most reliable would be create a wrapping class which would check file's size when you open it, track write and seek operations, count current size based on those operations and prevent from exceeding size limit.