On this page
  1. lzma
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Streaming .xz Text Files (lzma.open)
  7. Gotchas & Boundaries
  8. Common Functions
  9. Classes & Objects
  10. Constants & Attributes

lzma

Prev: logging | Up: stdlib_module | Next: mailbox
Rel: pickle, copyreg, shelve, marshal, dbm
Ref: docs.python.org — lzma
Tags: #public #python3 #stdlib


Import

import lzma

Overview

LZMA / .xz compression and decompression (compress, decompress, open, LZMAFile, LZMACompressor, LZMADecompressor) offering very high compression ratios for archival data, release tarballs (.tar.xz), and static corpora at the cost of higher CPU/memory during compression.

Quick Start

import lzma

raw = b"high-ratio archival payload " * 100
compressed = lzma.compress(raw)
assert len(compressed) < len(raw)
assert lzma.decompress(compressed) == raw

Implementation Walkthrough

1. Streaming .xz Text Files (lzma.open)

import lzma
from pathlib import Path
import tempfile

with tempfile.TemporaryDirectory() as tmp:
    p = Path(tmp) / "corpus.txt.xz"
    with lzma.open(p, "wt", encoding="utf-8", preset=6) as fh:
        fh.write("archived line\n")
    with lzma.open(p, "rt", encoding="utf-8") as fh:
        assert fh.read() == "archived line\n"

Gotchas & Boundaries

Common Functions

Classes & Objects

Constants & Attributes

Linked from

Related notes