如何在 Python 中确定打开文件的大小?

发布于 2024-08-13 20:50:19 字数 111 浏览 10 评论 0原文

我想确保有一个文件的大小不会超过 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 技术交流群。

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

发布评论

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

评论(7

空名 2024-08-20 20:50:19

也许不是你想要的,但无论如何我都会建议它。

import os
a = os.path.getsize("C:/TestFolder/Input/1.avi")

或者,对于打开的文件,您可以使用 fstat 函数,该函数可以是用于打开的文件。它需要一个整数文件句柄,而不是文件对象,因此您必须在文件对象上使用 fileno 方法:

a = open("C:/TestFolder/Input/1.avi")
b = os.fstat(a.fileno()).st_size

Perhaps not what you want, but I'll suggest it anyway.

import os
a = os.path.getsize("C:/TestFolder/Input/1.avi")

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:

a = open("C:/TestFolder/Input/1.avi")
b = os.fstat(a.fileno()).st_size
守不住的情 2024-08-20 20:50:19

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.

笔落惊风雨 2024-08-20 20:50:19

虽然这是一个老问题,但我认为 Isak 有最简单的解决方案。以下是在 Python 中执行此操作的方法:

# Assuming f is an open file
>>> pos = f.tell()  # Save the current position
>>> f.seek(0, 2)  # Seek to the end of the file
>>> length = f.tell()  # The current position is the length
>>> f.seek(pos)  # Return to the saved position
>>> print length
1024

Though this is an old question, I think that Isak has the simplest solution. Here's how to do it in Python:

# Assuming f is an open file
>>> pos = f.tell()  # Save the current position
>>> f.seek(0, 2)  # Seek to the end of the file
>>> length = f.tell()  # The current position is the length
>>> f.seek(pos)  # Return to the saved position
>>> print length
1024
夜还是长夜 2024-08-20 20:50:19

您可以从这样的开始:

class TrackedFile(file):
    def __init__(self, filename, mode):
        self.size = 0
        super(TrackedFile, self).__init__(filename, mode)
    def write(self, s):
        self.size += len(s)
        super(TrackedFile, self).write(s)

然后您可以像这样使用它:

>>> f = TrackedFile('palindrome.txt', 'w')
>>> f.size
0
>>> f.write('A man a plan a canal ')
>>> f.size
21
>>> f.write('Panama')
27

显然,如果您不是从头开始编写文件,则此实现不起作用,但您可以调整您的 __init__ 方法以处理初始数据。您可能还需要重写一些其他方法:例如 writelines

无论编码如何,这都有效,因为字符串只是字节序列。

>>> f2 = TrackedFile('palindrome-latin1.txt', 'w')
>>> f2.write(u'A man a plan a canál '.encode('latin1')
>>> f3 = TrackedFile('palindrome-utf8.txt', 'w')
>>> f3.write(u'A man a plan a canál '.encode('utf-8'))
>>> f2.size
21
>>> f3.size
22

You could start with something like this:

class TrackedFile(file):
    def __init__(self, filename, mode):
        self.size = 0
        super(TrackedFile, self).__init__(filename, mode)
    def write(self, s):
        self.size += len(s)
        super(TrackedFile, self).write(s)

Then you could use it like this:

>>> f = TrackedFile('palindrome.txt', 'w')
>>> f.size
0
>>> f.write('A man a plan a canal ')
>>> f.size
21
>>> f.write('Panama')
27

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.

>>> f2 = TrackedFile('palindrome-latin1.txt', 'w')
>>> f2.write(u'A man a plan a canál '.encode('latin1')
>>> f3 = TrackedFile('palindrome-utf8.txt', 'w')
>>> f3.write(u'A man a plan a canál '.encode('utf-8'))
>>> f2.size
21
>>> f3.size
22
幽梦紫曦~ 2024-08-20 20:50:19

我不熟悉 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.

微凉徒眸意 2024-08-20 20:50:19

或者,如果文件已打开:

>>> fsock = open('/etc/hosts', 'rb').read()
>>> len(fsock)
444

这就是文件的字节数。

Or, if the file is already open:

>>> fsock = open('/etc/hosts', 'rb').read()
>>> len(fsock)
444

That's how many bytes the file is.

剩一世无双 2024-08-20 20:50:19

最可靠的是创建一个包装类,它会在打开文件时检查文件的大小,跟踪写入和查找操作,根据这些操作计算当前大小并防止超出大小限制。

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.

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