转换 bytes to file-like object
来源:互联网 发布:梦里花落知多少的小说 编辑:程序博客网 时间:2024/05/21 10:49
引入:很多时候我们的一些函数的输入是file对象,然而我们现在能拿到的仅仅是文件的字节数组。则我们需要将bytes转换为文件这么一个过程。
一个现实的例子就是。Python的自带zip库zipfile可以对zip文件进行解压,压缩的工作。现在我想用ZipFile来处理我的zip包,有些特别的是这个zip包里面仍是一个个更小的zip包。 由于某种原因,对于每一个内部的小zip包进入打开操作ZipFile.open()返回的file-like对象有问题。这种返回的file-like对象被认为是不合法的zip文件。
为了绕过这个问题,我们只有通过使用ZipFile的read()方法读取小zip的字节数据。再而使用一种方法将此字节数据转换成file-like对象。这里我使用的是io.BytesIO库。
def __init__( self, filePath ): self.zf = ZipFile( filePath, "r", compression = zipfile.ZIP_DEFLATED ) self.zfinforlist = self.zf.infolist() def getNext( self ): readBytes = self.zf.read( self.zfinforlist[self.index] ) fileBytes = BytesIO( readBytes ) zf2 = ZipFile( fileBytes ) fileName = zf2.namelist()[0] zf2.read( fileName )
通过 fileBytes = BytesIO( readBytes ) 以及 zf2 = ZipFile( fileBytes )。我成功的将字节转换成为了file-like对象。且此对象呗认为是合法的zip文件。
- 转换 bytes to file-like object
- python 3.5: TypeError: a bytes-like object is required, not 'str' when writing to a file
- file转换成bytes
- TypeError: a bytes-like object is required, not 'str'
- python3 TypeError: a bytes-like object is required, not 'str'
- python3 TypeError: a bytes-like object is required, not 'str'
- TypeError: a bytes-like object is required, not 'str'
- 正则匹配href错误 expected string or bytes-like object
- TypeError: a bytes-like object is required, not 'str'
- a bytes-like object is required,not 'str'
- Error:a bytes-like object is required not str
- [IO] File Util print file to hex bytes
- can't use a string pattern on a bytes-like object错误和must be str, not bytes错误
- Python 出现 can't use a string pattern on a bytes-like object
- can't use a string pattern on a bytes-like object
- Python 出现 can't use a string pattern on a bytes-like object
- TypeError:can't use a string pattern on bytes-like object
- TypeError: cannot use a string pattern on a bytes-like object解决方法
- javascript控制input只能上传图片
- php 相关电子书之类的下载资料
- 重构 — 勿以善小而不为
- MyBatis学习 之 二、SQL语句映射文件(1)resultMap
- JSP中获得当前路径
- 转换 bytes to file-like object
- (.text+0xb3a): undefined reference to `clock_gettime'
- 嗖嗖嗖
- JAVA面试题-基础部分(一)
- iOS: HTTPS 与自签名证书
- 山寨和创新
- linux 共享文件夹配置
- Facebook的十点经验分享
- 二、SQL语句映射文件(2)增删改查、参数、缓存