【问题标题】:Python, how to read bytes from file and save it? [closed]Python,如何从文件中读取字节并保存? [关闭]
【发布时间】:2011-10-10 20:41:36
【问题描述】:

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

我该怎么做?

【问题讨论】:

    标签: python


    【解决方案1】:

    以下是在 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())
    

    如果不想将整个文件存储在内存中,可以分段传输。

    piece_size = 4096 # 4 KiB
    
    with open("in-file", "rb") as in_file, open("out-file", "wb") as out_file:
        while True:
            piece = in_file.read(piece_size)
    
            if piece == "":
                break # end of file
    
            out_file.write(piece)
    

    【讨论】:

    • 这里的问题是,如果文件很大,那么您将使用大量内存(例如,复制 2 GB 的文件!)
    • 以防万一其他人有这个问题,停止条件应该是:if piece == b''(空字节的双单引号)这个答案帮助了我很多,但它确实我整理东西时出现了一些无限循环。
    【解决方案2】:

    在我的示例中,我在打开文件时使用了“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)
    

    请参阅以下内容:

    【讨论】:

    • @MarkTolonen Doh!并固定...
    【解决方案3】:
    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 KB,然后写入。您可以通过这种方式支持非常大的文件,因为您不需要将整个文件读入内存。

    【讨论】:

      【解决方案4】:

      使用open 函数打开文件。 open函数返回一个file object,你可以使用它来读写文件:

      file_input = open('input.txt') #opens a file in reading mode
      file_output = open('output.txt') #opens a file in writing mode
      
      data = file_input.read(1024) #read 1024 bytes from the input file
      file_output.write(data) #write the data to the output file
      

      【讨论】:

        猜你喜欢
        • 2010-09-14
        • 2013-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        相关资源
        最近更新 更多