14

I was trying to decompress a 7z file using python, but I can't seem to figure it out. I figured I could use the lzma module in python 3, but I can't seem to figure it out:

I thought it would work like the zipfile package:

import lzma
with lzma.open('data.7z') as f:
    f.extractall(r"<output path>")

but after reading the documents, it doesn't seems to. So here is my question: How can you extract a 7z file using the standard package? I don't want to call subprocess to extract the files using 7-zip because I can't guarantee that users have this software installed.

I've searched the internets and stack oerflow and noticed all the answers almost go back to using subprocessing which I would like to avoid like the plague.

Though there are similar questions on stackoverflow, the answers all still depend on 7-zip or the 7zip SDK. I do not want to use the 7-zip sdk/exe for extraction because that assumes the users have the software installed.

Here is the properties from the 7z file:

enter image description here

4
  • @Tezirg this is for python 2 not 3 Commented May 23, 2017 at 10:55
  • executing the above code, what output do you get ? any exceptions ? Commented May 23, 2017 at 14:04
  • 1
    @NabeelAhmed builtins.AttributeError: 'LZMAFile' object has no attribute 'extractall' Commented May 23, 2017 at 14:34
  • I think that this might be a duplicate of: Python3: lzma unpack .7z file
    – pgmank
    Commented Sep 22, 2017 at 20:41

1 Answer 1

24

What about trying this?:

from pyunpack import Archive
Archive('data.7z').extractall("<output path>")
2
  • 10
    Marvelous, after pip install pyunpack and pip install patool it worked excellent!
    – a.t.
    Commented Aug 21, 2020 at 19:13
  • also, please, apt-get install p7zip if you use debian
    – Wera
    Commented Jun 13 at 8:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.