Python编写二进制文件,字节

Python编写二进制文件,字节

问题描述:

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

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.

示例代码:

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

此逻辑适用于文本文件当不使用'b'指示符时,或者从web上读取文件时,例如urllib或者请求。这些是字节类型,我认为我需要打开文件。相反,它作为缓冲读者开放。我尝试了字节(f1),但得到TypeError:'bytes'对象不能被解释为整数。任何想法?

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?

如果你的目的是简单地复制文件,你可以使用 shutil

If your intent is to simply make a copy of the file, you could use 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

但是逐字节可能非常慢

或者,如果你想要一个缓冲器来加速它(不冒冒险读取的话)未知文件大小完全进入内存):

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

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

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

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