读取二进制文件并在每个文件上循环

2024-04-20 14:09:35 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
3条回答

如果文件不太大,则将其保存在内存中是一个问题:

with open("filename", "rb") as f:
    bytes_read = f.read()
for b in bytes_read:
    process_byte(b)

其中process_byte表示要对传入字节执行的操作。

如果要一次处理块:

with open("filename", "rb") as f:
    bytes_read = f.read(CHUNKSIZE)
    while bytes_read:
        for b in bytes_read:
            process_byte(b)
        bytes_read = f.read(CHUNKSIZE)

Python 2.5及更高版本中提供了with语句。

Python2.4及更早版本

f = open("myfile", "rb")
try:
    byte = f.read(1)
    while byte != "":
        # Do stuff with byte.
        byte = f.read(1)
finally:
    f.close()

Python2.5-2.7

with open("myfile", "rb") as f:
    byte = f.read(1)
    while byte != "":
        # Do stuff with byte.
        byte = f.read(1)

注意with语句在低于2.5的Python版本中不可用。要在v 2.5中使用它,您需要导入它:

from __future__ import with_statement

在2.6中,这是不需要的。

Python 3

在Python 3中,它有点不同。我们将不再以字节模式从流中获取原始字符,而是获取字节对象,因此需要更改条件:

with open("myfile", "rb") as f:
    byte = f.read(1)
    while byte != b"":
        # Do stuff with byte.
        byte = f.read(1)

或者正如benhoyt所说,跳过not equal并利用b""计算结果为false这一事实。这使得代码在2.6和3.x之间兼容,没有任何更改。如果从字节模式转到文本模式或相反模式,也可以避免更改条件。

with open("myfile", "rb") as f:
    byte = f.read(1)
    while byte:
        # Do stuff with byte.
        byte = f.read(1)

此生成器从文件中生成字节,以块的形式读取文件:

def bytes_from_file(filename, chunksize=8192):
    with open(filename, "rb") as f:
        while True:
            chunk = f.read(chunksize)
            if chunk:
                for b in chunk:
                    yield b
            else:
                break

# example:
for b in bytes_from_file('filename'):
    do_stuff_with(b)

有关iteratorsgenerators的信息,请参阅Python文档。

相关问题 更多 >