How to read contents of 7z file using python

Ken Kem picture Ken Kem · Sep 26, 2015 · Viewed 64.1k times · Source

How can I read and save the contents of 7z. I use Python 2.7.9, I can extract or Archive like this, but I can't read contents in python, I only listing the file's contents in CMD

import subprocess
import os

source = 'filename.7z'
directory = 'C:\Directory'
pw = '123456'
subprocess.call(r'"C:\Program Files (x86)\7-Zip\7z.exe" x '+source +' -o'+directory+' -p'+pw)

Answer

mr nick picture mr nick · Sep 26, 2015

You can use either libarchive or pylzma. If you can upgrade to python3.3+ you can use lzma, which is in the standard library.