How to use pyunpack to unpack .7z file?

Question:

Im trying to unpack a 7z file but got an error.

This is the code:

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

And this is error:

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

How can I fix it?

Asked By: Fatworm

||

Answers:

You can instead use the combination of py7zr package and shutil package to unzip 7z file.

Steps

  1. Pip install py7zr

  2. Run the below code:

from py7zr import unpack_7zarchive
import shutil

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

If you are using Mac OS, install 7zip with command

brew install p7zip

On windows download and install 7zip from https://www.7-zip.org/download.html

Then update the PATH variable to point the directory containing 7za.exe

Answered By: Nikhil George
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.