Python 3.3+lzmafunction
lzma.open()
files
Opens an .xz or .lzma file in binary or text mode — the counterpart of open() for LZMA-compressed files.
Syntax
f = lzma.open(filename, mode='rb', *, format=None, check=-1, preset=None, filters=None, encoding=None, errors=None, newline=None)
Like bz2.open(), it opens in 'rb' by default and returns bytes; for strings use 'rt'/'wt' with an explicit encoding. The format is detected on reading, so one function reads both .xz and legacy .lzma. When writing, the format parameter picks the container — the file name's extension has no effect.
Examples
import lzma
with lzma.open('old.lzma', 'wt', encoding='utf-8', format=lzma.FORMAT_ALONE) as f:
f.write('строка')
with lzma.open('old.lzma', 'rt', encoding='utf-8') as f:
print(f.read()) # → строка
See also
Open in the full glossarydocs.python.orgReport an inaccuracy