本文介绍了Python3:lzma 解压 .7z 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解压一个 .7z 文件.根据这个问题我可以使用lzma 包来执行此操作.

I would like to unpack a .7z file. According to this question I can use thelzma package to do this.

我期待类似的东西

import lzma
#...
with lzma.open('myFile.7z') as f:
    f.extractall('.')

把文件解压到当前目录但是好像不存在这样的东西.此外尝试像

To extract the file into the current directory but it seems something like this does not exist. Furthermore trying something like

import lzma
#...
with lzma.open('myFile.7z') as f:
    file_content = f.read()
    print(file_content)

确实产生了_lzma.LZMAError:解码器不支持输入格式.如何检查格式?我很惊讶,因为我认为 7zip 和 .7z 格式都是开源的,python 应该支持一切.

did yield _lzma.LZMAError: Input format not supported by decoder. How can I check the format? And I am quite surprised because I thought both 7zip and the .7z format are open source and python should support everything.

我看到很多答案,人们只是通过子进程调用 7zip 可执行文件,但这不是我想要的.我正在寻找一个简单的 python3 解决方案.

I saw a lot of answers where people were just calling the 7zip executable with a subprocess but this is not want I want to do. I am looking for a plain python3 solution.

推荐答案

LZMA 和 7z 是两个 请参阅下面的更新.

import os
os.system( '7z x archive.7z -oPath/to/Name' )

更新:2019 年 5 月

由于有人对在 python 中提取 7z 文件感兴趣,我认为应该进行更新.截至 2019 年(也许更早),python 的 libarchive bindings 支持 7z 格式.上面的链接给出了从 7z 存档中提取文件的示例.

Update: May 2019

Since there some interest about extracting 7z files in python, I thought an update is in order. As of 2019 (perhaps even earlier), libarchive bindings for python do support 7z format. An example for extracting files from 7z archive is given in above link.

这篇关于Python3:lzma 解压 .7z 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:13
查看更多