python解压bz2文件命令_解压缩bz2文件
大家好,又见面了,我是你们的朋友全栈君。
bz2.compress/decompress使用二进制数据:>>> import bz2
>>> compressed = bz2.compress(b’test_string’)
>>> compressed
b’BZh91AY&SYJ|i\x05\x00\x00\x04\x83\x80\x00\x00\x82\xa1\x1c\x00 \x00″\x03h\x840″
P\xdf\x04\x99\xe2\xeeH\xa7\n\x12\tO\x8d \xa0′
>>> bz2.decompress(compressed)
b’test_string’
简而言之-您需要手动处理文件内容。如果您有非常大的文件,您应该使用bz2.BZ2Decompressor而不是bz2.decompress,因为后者要求您将整个文件存储在字节数组中。for filename in files:
filepath = os.path.join(dirpath, filename)
newfilepath = os.path.join(dirpath,filename + ‘.decompressed’)
with open(newfilepath, ‘wb’) as new_file, open(filepath, ‘rb’) as file:
decompressor = BZ2Decompressor()
for data in iter(lambda : file.read(100 * 1024), b”):
new_file.write(decompressor.decompress(data))
您还可以使用bz2.BZ2File来简化此过程:for filename in files:
filepath = os.path.join(dirpath, filename)
newfilepath = os.path.join(dirpath, filename + ‘.decompressed’)
with open(newfilepath, ‘wb’) as new_file, bz2.BZ2File(filepath, ‘rb’) as file:
for data in iter(lambda : file.read(100 * 1024), b”):
new_file.write(data)
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/135032.html原文链接:https://javaforall.cn
相关文章
- python上传文件方法总结
- python处理xps文件_xps/pdf/png/json转换
- Python 创建加密压缩文件「建议收藏」
- python怎么读取xlsx文件_arcgis地理加权回归
- 为什么python读取不了文件_python系统找不到指定文件怎么办
- Python删除文件到回收站
- python写入txt文件中文乱码_python中怎么输入文件
- python删除文件中指定内容
- 用 Python 破解了同学压缩文件的密码
- python 删除文件中的空行
- Python 打开文件对话框「建议收藏」
- Python实战-暴力破解zip文件解压密码
- python强制删除文件夹_python删除文件夹下的文件保留但清空子文件夹
- python读取txt文件中的json数据
- python读取文件如何去除空格_python读取txt文件时怎么去掉空格
- python–xlsx文件的读写[通俗易懂]
- python删除行_python 删除文件中指定行
- Python 调用接口进行文件上传的踩坑记录
- Python之文件操作大全
- python读写、创建文件、文件夹等等