Python写入二进制文件,字节

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16630789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:12:40  来源:igfitidea点击:

Python writing binary files, bytes

pythonpython-3.xiobufferbufferedreader

提问by Turtles Are Cute

Python 3. I'm using QT's file dialog widget to save PDFs downloaded from the internet. I've been reading the file using 'open', and attempting to write it using the file dialog widget. However, I've been running into a"TypeError: '_io.BufferedReader' does not support the buffer interface" error.

Python 3. 我正在使用 QT 的文件对话框小部件来保存从 Internet 下载的 PDF。我一直在使用“打开”读取文件,并尝试使用文件对话框小部件编写它。但是,我遇到了“TypeError: '_io.BufferedReader' 不支持缓冲区接口”错误。

Example code:

示例代码:

with open('file_to_read.pdf', 'rb') as f1: 
    with open('file_to_save.pdf', 'wb') as f2:
        f2.write(f1)

This logic works properly with text files when not using the 'b' designator, or when reading a file from the web, like with urllib or requests. These are of the 'bytes' type, which I think I need to be opening the file as. Instead, it's opening as a Buffered Reader. I tried bytes(f1), but get "TypeError: 'bytes' object cannot be interpreted as an integer." Any ideaas?

当不使用“b”指示符或从网络读取文件时(如使用 urllib 或 requests),此逻辑适用于文本文件。这些是“字节”类型,我认为我需要将文件打开。相反,它作为缓冲阅读器打开。我试过 bytes(f1),但得到“TypeError: 'bytes' object cannot be interpret as an integer。” 有什么想法吗?

采纳答案by dawg

If your intent is to simply make a copy of the file, you could use shutil

如果您的目的只是制作文件的副本,则可以使用shutil

>>> import shutil
>>> shutil.copyfile('file_to_read.pdf','file_to_save.pdf')

Or if you need to access byte by byte, similar to your structure, this works:

或者,如果您需要逐字节访问,类似于您的结构,这有效:

>>> with open('/tmp/fin.pdf','rb') as f1:
...    with open('/tmp/test.pdf','wb') as f2:
...       while True:
...          b=f1.read(1)
...          if b: 
...             # process b if this is your intent   
...             n=f2.write(b)
...          else: break

But byte by byte is potentially really slow.

但是逐字节可能真的很慢

Or, if you want a buffer that will speed this up (without taking the risk of reading an unknown file size completely into memory):

或者,如果您想要一个可以加快速度的缓冲区(而不会有将未知文件大小完全读入内存的风险):

>>> with open('/tmp/fin.pdf','rb') as f1:
...    with open('/tmp/test.pdf','wb') as f2:
...       while True:
...          buf=f1.read(1024)
...          if buf: 
...              for byte in buf:
...                 pass    # process the bytes if this is what you want
...                         # make sure your changes are in buf
...              n=f2.write(buf)
...          else:
...              break

With Python 2.7+ or 3.1+ you can also use this shortcut (rather than using two withblocks):

对于 Python 2.7+ 或 3.1+,您还可以使用此快捷方式(而不是使用两个with块):

with open('/tmp/fin.pdf','rb') as f1,open('/tmp/test.pdf','wb') as f2:
    ...

回答by Kritzefitz

It really doesn't make sense to write a file in another file. What you want is to write the contents of f1 in f2. You get the contents with f1.read(). So you have to do this:

将一个文件写入另一个文件中确实没有意义。你想要的是将f1的内容写入f2。您可以使用 f1.read() 获取内容。所以你必须这样做:

with open('file_to_read.pdf', 'rb') as f1: 
    with open('file_to_save.pdf', 'wb') as f2:
        f2.write(f1.read())

回答by WeizhongTu

learned from python cookbook

学习了 python cookbook

from functools import partial

with open(fpath, 'rb') as f, open(target_fpath, 'wb') as target_f: 
    for _bytes in iter(partial(f.read, 1024), ''):
        target_f.write(_bytes)

partial(f.read, 1024)returns a function, read the binary file 1024 bytes at every turn. iterwill end when meet a blank string ''.

partial(f.read, 1024)返回一个函数,每次读取二进制文件1024字节。iter将在遇到 a 时结束blank string ''