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:
>>> 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()andlzma.decompress() - Reads and writes
.xzand legacy.lzmafiles throughlzma.open()andLZMAFile - 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
LZMACompressorandLZMADecompressorfor 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:
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:
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:
$ python read_notes.py
Plain text goes in
and gets compressed.
Compressing data incrementally with LZMACompressor:
>>> 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:
>>> 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
.xzarchives produced by the Unix xz utility - Shrinking large text datasets such as CSV, JSON, or log dumps for long-term storage
- Decompressing legacy
.lzmafiles 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:
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.
Related Resources
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.
For additional information on related topics, take a look at the following resources:
- Reading and Writing Files in Python (Guide) (Tutorial)
- Bytes Objects: Handling Binary Data in Python (Tutorial)
- Unicode & Character Encodings in Python: A Painless Guide (Tutorial)
- How to Convert Bytes to Strings in Python (Tutorial)
- Reading and Writing CSV Files in Python (Tutorial)
- Manipulating ZIP Files With Python (Course)
- Reading and Writing Files in Python (Course)
- Reading and Writing Files in Python (Quiz)
- Python Bytes (Quiz)
- Unicode in Python: Working With Character Encodings (Course)
- How to Convert Bytes to Strings in Python (Quiz)
- Reading and Writing CSV Files (Course)
- Reading and Writing CSV Files in Python (Quiz)
By Leodanis Pozo Ramos • Updated April 23, 2026