Python,如何从文件中读取字节并保存?

80 投票
4 回答
273996 浏览
提问于 2025-04-16 22:03

我想从一个文件里读取字节,然后把这些字节写入另一个文件,并保存这个文件。

我该怎么做呢?

4 个回答

7
with open("input", "rb") as input:
    with open("output", "wb") as output:
        while True:
            data = input.read(1024)
            if data == "":
                break
            output.write(data)

上面的代码每次会读取1千字节的数据,然后写入。这样做可以处理非常大的文件,因为你不需要把整个文件都加载到内存里。

19

在我的例子中,我使用了'b'这个标志('wb', 'rb')来打开文件,因为你说你想读取字节。这个'b'标志告诉Python不要去处理换行符,因为不同的操作系统换行符可能不一样。如果你是读取文本,就可以去掉'b',直接用'w'和'r'。

这个方法是用最简单的Python代码一次性读取整个文件。这个方法的问题在于,如果文件很大,可能会导致内存不足:

ifile = open(input_filename,'rb')
ofile = open(output_filename, 'wb')
ofile.write(ifile.read())
ofile.close()
ifile.close()

这个例子改进了,分成1MB的小块来读取,这样就能确保处理任何大小的文件而不会耗尽内存:

ifile = open(input_filename,'rb')
ofile = open(output_filename, 'wb')
data = ifile.read(1024*1024)
while data:
    ofile.write(data)
    data = ifile.read(1024*1024)
ofile.close()
ifile.close()

这个例子和上面的差不多,但使用了with来创建一个上下文。这个方法的好处是,当你退出这个上下文时,文件会自动关闭:

with open(input_filename,'rb') as ifile:
    with open(output_filename, 'wb') as ofile:
        data = ifile.read(1024*1024)
        while data:
            ofile.write(data)
            data = ifile.read(1024*1024)

请看以下链接:

154

下面是如何使用Python的基本文件操作来完成这个任务。首先打开一个文件,把数据读到内存里,然后再打开第二个文件,把数据写出去。

in_file = open("in-file", "rb") # opening for [r]eading as [b]inary
data = in_file.read() # if you only wanted to read 512 bytes, do .read(512)
in_file.close()

out_file = open("out-file", "wb") # open for [w]riting as [b]inary
out_file.write(data)
out_file.close()

我们可以用with这个关键词来更简洁地处理文件的关闭问题。

with open("in-file", "rb") as in_file, open("out-file", "wb") as out_file:
    out_file.write(in_file.read())

如果你不想把整个文件都放在内存里,可以分块传输数据。

chunk_size = 4096 # 4 KiB

with open("in-file", "rb") as in_file, open("out-file", "wb") as out_file:
    while True:
        chunk = in_file.read(chunk_size)
            
        if chunk == b"":
            break # end of file
            
        out_file.write(chunk)

撰写回答