tarfile — 读写 tar 存档文件

源代码: Lib/tarfile.py


The tarfile module makes it possible to read and write tar archives, including those using gzip, bz2 and lzma compression. Use the zipfile module to read or write .zip files, or the higher-level functions in shutil .

Some facts and figures:

  • 读写 gzip , bz2 and lzma compressed archives if the respective modules are available.

  • read/write support for the POSIX.1-1988 (ustar) format.

  • read/write support for the GNU tar format including longname and longlink extensions, read-only support for all variants of the sparse extension including restoration of sparse files.

  • read/write support for the POSIX.1-2001 (pax) format.

  • handles directories, regular files, hardlinks, symbolic links, fifos, character devices and block devices and is able to acquire and restore file information like timestamp, access permissions and owner.

3.3 版改变: 添加支持 lzma 压缩。

3.12 版改变: Archives are extracted using a filter , which makes it possible to either limit surprising/dangerous features, or to acknowledge that they are expected and the archive is fully trusted. By default, archives are fully trusted, but this default is deprecated and slated to change in Python 3.14.