All formats

py7zr

LanguagePython 3.10+
LicenseLGPL-2.1-or-later
Repositorygithub.com/miurahr/py7zr
Documentationpy7zr.readthedocs.io
Depends onpycryptodome, pyppmd, pybcj, brotli, inflate64, multivolumefile, texttable, psutil, and backports.zstd before Python 3.14 (installed automatically)
Read / writeyes / yes; LZMA, LZMA2, BZip2, Deflate, PPMd, Zstandard, Brotli, Copy, BCJ, Delta, AES-256; Deflate64 read only; no BCJ2

py7zr reads and writes 7z archives with an API like zipfile and tarfile: SevenZipFile(path, mode) as a context manager, extractall, getnames, write, writeall. It supports solid archives, encrypted data and encrypted headers, and a command line (py7zr x archive.7z). It also provides functions to plug 7z into shutil.unpack_archive and shutil.make_archive.

The format logic is pure Python; the compression codecs come from compiled packages on PyPI (pyppmd, pybcj, brotli, inflate64) and the standard lzma, bz2, zlib and (on Python 3.14) compression.zstd modules. It’s slower than 7-Zip, especially for large archives, but needs no external program. It can’t read archives that use BCJ2 (7-Zip uses it for executables at high compression levels).

Installation#

Shell
pip install py7zr

Basic usage: extract and create a 7z archive#

Python
import py7zr

# extract everything
with py7zr.SevenZipFile("archive.7z", mode="r") as archive:
    archive.extractall(path="output")

# create a new archive from a folder and a string
with py7zr.SevenZipFile("backup.7z", mode="w") as archive:
    archive.writeall("photos", arcname="photos")      # folder, recursively
    archive.write("report.pdf", arcname="docs/report.pdf")
    archive.writestr("created by my app\n", "notes/readme.txt")

Mode "a" appends files to an existing archive, "x" creates and fails if the file exists.

How to list the contents of a 7z file#

Python
import py7zr

with py7zr.SevenZipFile("archive.7z", mode="r") as archive:
    for info in archive.list():
        print(info.filename, info.uncompressed, info.is_directory, info.creationtime)
    print(archive.getnames())

    meta = archive.archiveinfo()
    print(meta.method_names, "solid" if meta.solid else "non-solid", meta.uncompressed)

py7zr.is_7zfile(path) checks the signature without opening the archive.

How to extract specific files#

Python
with py7zr.SevenZipFile("archive.7z", mode="r") as archive:
    archive.extract(path="output", targets=["docs/report.pdf", "notes/readme.txt"])

targets must be a list (or other collection), not a single string.

How to read a file from a 7z archive into memory#

Pass a WriterFactory to extract; BytesIOFactory keeps each extracted file in a BytesIO, up to limit bytes:

Python
import py7zr
from py7zr.io import BytesIOFactory

factory = BytesIOFactory(limit=64 * 1024 * 1024)
with py7zr.SevenZipFile("archive.7z", mode="r") as archive:
    archive.extract(targets=["notes/readme.txt"], factory=factory)

buf = factory.get("notes/readme.txt")
buf.seek(0)
data = buf.read()

How to create and extract a password-protected 7z archive#

Python
import py7zr

with py7zr.SevenZipFile("secret.7z", mode="w", password="secret") as archive:
    archive.set_encrypted_header(True)   # also hide file names
    archive.writeall("private", arcname="private")

with py7zr.SevenZipFile("secret.7z", mode="r", password="secret") as archive:
    archive.extractall(path="output")

Opening an encrypted archive without a password raises py7zr.PasswordRequired. Check with archive.needs_password().

How to set the compression method and level#

Python
import py7zr
from py7zr import FILTER_LZMA2, FILTER_ZSTD, FILTER_X86, PRESET_DEFAULT

# LZMA2 at level 9 (smallest, slowest)
with py7zr.SevenZipFile("small.7z", "w", filters=[{"id": FILTER_LZMA2, "preset": 9}]) as archive:
    archive.writeall("data", arcname="data")

# x86 filter + LZMA2 for executables
filters = [{"id": FILTER_X86}, {"id": FILTER_LZMA2, "preset": PRESET_DEFAULT}]

# Zstandard: fast, but only 7-Zip ZS / NanaZip and py7zr open it, not standard 7-Zip
filters = [{"id": FILTER_ZSTD, "level": 3}]

How to use 7z with shutil#

Python
import shutil
from py7zr import pack_7zarchive, unpack_7zarchive

shutil.register_archive_format("7zip", pack_7zarchive, description="7zip archive")
shutil.register_unpack_format("7zip", [".7z"], unpack_7zarchive)

shutil.make_archive("backup", "7zip", "data")   # creates backup.7z from the data folder
shutil.unpack_archive("backup.7z", "restored")

How to check an archive for errors#

Python
with py7zr.SevenZipFile("archive.7z", "r") as archive:
    bad = archive.testzip()   # name of the first file with a CRC error, or None

Tips#