### 导航
- [索引](../genindex.xhtml "总目录")
- [模块](../py-modindex.xhtml "Python 模块索引") |
- [下一页](zipfile.xhtml "zipfile --- 在 ZIP 归档中工作") |
- [上一页](bz2.xhtml "bz2 --- 对 bzip2 压缩算法的支持") |
- ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png)
- [Python](https://www.python.org/) »
- zh\_CN 3.7.3 [文档](../index.xhtml) »
- [Python 标准库](index.xhtml) »
- [数据压缩和存档](archiving.xhtml) »
- $('.inline-search').show(0); |
# [`lzma`](#module-lzma "lzma: A Python wrapper for the liblzma compression library.") --- 用 LZMA 算法压缩
3\.3 新版功能.
**源代码:** [Lib/lzma.py](https://github.com/python/cpython/tree/3.7/Lib/lzma.py) \[https://github.com/python/cpython/tree/3.7/Lib/lzma.py\]
- - - - - -
This module provides classes and convenience functions for compressing and decompressing data using the LZMA compression algorithm. Also included is a file interface supporting the `.xz` and legacy `.lzma` file formats used by the **xz** utility, as well as raw compressed streams.
The interface provided by this module is very similar to that of the [`bz2`](bz2.xhtml#module-bz2 "bz2: Interfaces for bzip2 compression and decompression.")module. However, note that [`LZMAFile`](#lzma.LZMAFile "lzma.LZMAFile") is *not* thread-safe, unlike [`bz2.BZ2File`](bz2.xhtml#bz2.BZ2File "bz2.BZ2File"), so if you need to use a single [`LZMAFile`](#lzma.LZMAFile "lzma.LZMAFile") instance from multiple threads, it is necessary to protect it with a lock.
*exception* `lzma.``LZMAError`This exception is raised when an error occurs during compression or decompression, or while initializing the compressor/decompressor state.
## Reading and writing compressed files
`lzma.``open`(*filename*, *mode="rb"*, *\**, *format=None*, *check=-1*, *preset=None*, *filters=None*, *encoding=None*, *errors=None*, *newline=None*)Open an LZMA-compressed file in binary or text mode, returning a [file object](../glossary.xhtml#term-file-object).
The *filename* argument can be either an actual file name (given as a [`str`](stdtypes.xhtml#str "str"), [`bytes`](stdtypes.xhtml#bytes "bytes") or [path-like](../glossary.xhtml#term-path-like-object) object), in which case the named file is opened, or it can be an existing file object to read from or write to.
The *mode* argument can be any of `"r"`, `"rb"`, `"w"`, `"wb"`, `"x"`, `"xb"`, `"a"` or `"ab"` for binary mode, or `"rt"`, `"wt"`, `"xt"`, or `"at"` for text mode. The default is `"rb"`.
When opening a file for reading, the *format* and *filters* arguments have the same meanings as for [`LZMADecompressor`](#lzma.LZMADecompressor "lzma.LZMADecompressor"). In this case, the *check*and *preset* arguments should not be used.
When opening a file for writing, the *format*, *check*, *preset* and *filters* arguments have the same meanings as for [`LZMACompressor`](#lzma.LZMACompressor "lzma.LZMACompressor").
For binary mode, this function is equivalent to the [`LZMAFile`](#lzma.LZMAFile "lzma.LZMAFile")constructor: `LZMAFile(filename, mode, ...)`. In this case, the *encoding*, *errors* and *newline* arguments must not be provided.
For text mode, a [`LZMAFile`](#lzma.LZMAFile "lzma.LZMAFile") object is created, and wrapped in an [`io.TextIOWrapper`](io.xhtml#io.TextIOWrapper "io.TextIOWrapper") instance with the specified encoding, error handling behavior, and line ending(s).
在 3.4 版更改: Added support for the `"x"`, `"xb"` and `"xt"` modes.
在 3.6 版更改: 接受一个 [类路径对象](../glossary.xhtml#term-path-like-object)。
*class* `lzma.``LZMAFile`(*filename=None*, *mode="r"*, *\**, *format=None*, *check=-1*, *preset=None*, *filters=None*)Open an LZMA-compressed file in binary mode.
An [`LZMAFile`](#lzma.LZMAFile "lzma.LZMAFile&#