如何使用pyunpack解压缩.7z文件?

时间:2018-06-07 15:51:22

标签: python python-2.7

我试图解压缩7z文件但出错了。

这是代码:

from pyunpack import Archive
Archive('E:/Desktop/vnpt2/2_1_0_2841.7z').extractall('E:/Desktop/vnpt2/new')

这是错误:

Traceback (most recent call last):
  File "E:\Desktop\vnpt2\zip.py", line 2, in <module>
    Archive('E:/Desktop/vnpt2/2_1_0_2841.7z').extractall('E:/Desktop/vnpt2/new')
  File "C:\Python27\lib\site-packages\pyunpack\__init__.py", line 90, in extractall
    self.extractall_patool(directory, patool_path)
  File "C:\Python27\lib\site-packages\pyunpack\__init__.py", line 62, in extractall_patool
    raise PatoolError('patool can not unpack\n' + str(p.stderr))
pyunpack.PatoolError: patool can not unpack
patool error: error extracting E:\Desktop\vnpt2\2_1_0_2841.7z: could not find an executable program to extract format 7z; candidates are (7z,7za,7zr),

我该如何解决?

2 个答案:

答案 0 :(得分:0)

您可以改为使用py7zr软件包和shutil软件包的组合来解压缩7z文件。

步骤

  1. Pip安装py7zr

  2. 运行以下代码:

from py7zr import unpack_7zarchive
import shutil

shutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive)
shutil.unpack_archive('filename.7z', '/unzip_path')

答案 1 :(得分:0)

如果您使用的是Mac OS,请使用命令安装7zip

brew install p7zip

在Windows上,从https://www.7-zip.org/download.html下载并安装7zip

然后更新PATH变量以指向包含7za.exe的目录