Python写入二进制文件,字节 [英] Python writing binary files, bytes

查看:61
本文介绍了Python写入二进制文件,字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 3.我正在使用 QT 的文件对话框小部件来保存从 Internet 下载的 PDF.我一直在使用打开"读取文件,并尝试使用文件对话框小部件编写它.但是,我遇到了TypeError: '_io.BufferedReader' does not support the buffer interface"错误.

示例代码:

 with open('file_to_read.pdf', 'rb') as f1:使用 open('file_to_save.pdf', 'wb') 作为 f2:f2.写(f1)

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

解决方案

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

<预><代码>>>>进口木材>>>Shutil.copyfile('file_to_read.pdf','file_to_save.pdf')

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

<预><代码>>>>使用 open('/tmp/fin.pdf','rb') 作为 f1:...使用 open('/tmp/test.pdf','wb') 作为 f2:...而真:... b=f1.read(1)...如果 b:... # process b 如果这是您的意图... n=f2.write(b)...否则:打破

但是逐字节可能真的很慢.

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

<预><代码>>>>使用 open('/tmp/fin.pdf','rb') 作为 f1:...使用 open('/tmp/test.pdf','wb') 作为 f2:...而真:... buf=f1.read(1024)...如果buf:... 对于 buf 中的字节:... pass # 处理字节,如果这是你想要的... # 确保您的更改在 buf 中... n=f2.write(buf)... 别的:... 休息

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

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

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.

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?

解决方案

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

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 with blocks):

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

这篇关于Python写入二进制文件,字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆