Skip to content

lzma

The Python lzma module provides classes and convenience functions for compressing and decompressing data with the LZMA algorithm, and it can read or write .xz and legacy .lzma files produced by the xz utility. Its interface closely mirrors the bz2 module, so similar patterns work for both.

Here is a quick in-memory round trip:

Language: Python
>>> import lzma

>>> blob = lzma.compress(b"Compress me!")
>>> blob[:6]
b'\xfd7zXZ\x00'
>>> lzma.decompress(blob)
b'Compress me!'

The first bytes, \xfd7zXZ\x00, are the magic number that identifies every .xz stream.

Key Features

  • Compresses and decompresses bytes objects in memory with lzma.compress() and lzma.decompress()
  • Reads and writes .xz and legacy .lzma files through lzma.open() and LZMAFile
  • Supports binary and text modes, including the "x" exclusive-creation mode
  • Offers nine preset compression levels, plus an extreme flag for even smaller output
  • Provides LZMACompressor and LZMADecompressor for incremental streaming
  • Accepts custom filter chains that combine LZMA with delta or BCJ transformations
  • Includes optional integrity checks such as CRC32, CRC64, and SHA-256

Frequently Used Classes and Functions

Object Type Description
lzma.open() Function Opens an LZMA-compressed file in binary or text mode
lzma.LZMAFile Class Provides a file-like object for reading and writing LZMA streams
lzma.compress() Function Compresses a bytes object and returns the compressed data
lzma.decompress() Function Decompresses LZMA data and returns the original bytes
lzma.LZMACompressor Class Compresses data incrementally in chunks
lzma.LZMADecompressor Class Decompresses data incrementally in chunks
lzma.LZMAError Exception Signals an error during compression or decompression

Examples

Writing a text file transparently compressed with LZMA:

Language: Python Filename: write_notes.py
import lzma

with lzma.open("notes.txt.xz", "wt", encoding="utf-8") as f:
    f.write("Plain text goes in\n")
    f.write("and gets compressed.\n")

Reading the same file back into a string:

Language: Python Filename: read_notes.py
import lzma

with lzma.open("notes.txt.xz", "rt", encoding="utf-8") as f:
    print(f.read())

Run it on the command line:

Language: Shell
$ python read_notes.py
Plain text goes in
and gets compressed.

Compressing data incrementally with LZMACompressor:

Language: Python
>>> import lzma

>>> lzc = lzma.LZMACompressor()
>>> chunks = [
...    lzc.compress(b"Some data\n"),
...    lzc.compress(b"More data\n"),
...    lzc.flush()
... ]

>>> compressed = b"".join(chunks)
>>> lzma.decompress(compressed)
b'Some data\nMore data\n'

Choosing a higher preset for smaller output:

Language: Python
>>> import lzma

>>> payload = b"Real Python reference article." * 100
>>> default_output = lzma.compress(payload, preset=0)
>>> smaller_output = lzma.compress(payload, preset=9 | lzma.PRESET_EXTREME)
>>> len(smaller_output) <= len(default_output)
True

Common Use Cases

The most common tasks for lzma include:

  • Reading and writing .xz archives produced by the Unix xz utility
  • Shrinking large text datasets such as CSV, JSON, or log dumps for long-term storage
  • Decompressing legacy .lzma files from older tools
  • Streaming compressed data across a network or pipeline one chunk at a time
  • Building custom filter chains to squeeze extra ratio out of binary or structured data

Real-World Example

Consider a scenario where you want to archive an existing plain-text log file into an .xz container without loading the whole file into memory. You can pair lzma.open() with shutil.copyfileobj() to stream the data through in fixed-size chunks:

Language: Python Filename: compress_log.py
import lzma
import shutil

with open("access.log", "rb") as f_in:
    with lzma.open("access.log.xz", "wb", preset=6) as f_out:
        shutil.copyfileobj(f_in, f_out)

After running the script, access.log.xz holds the same contents as access.log, but in a form that any xz-aware tool can read. Because both files are opened as context managers, their handles close automatically when the with blocks exit, even if an error occurs mid-copy.

Tutorial

Python's zipfile: Manipulate Your ZIP Files Efficiently

In this guided tutorial, you'll learn how to manipulate ZIP files using Python's zipfile module from the standard library. Through hands-on examples, you'll learn how to read, write, compress, and extract files from your ZIP files quickly.

intermediate python stdlib

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated April 23, 2026