Python Glossary Русский

all Python 3tarfileterm

tarfile

module

Read and write tar archives, including gzip, bz2, xz and zstd compressed ones.

Syntax

import tarfile

tarfile handles .tar, .tar.gz and related archives common on Unix; for .zip there is zipfile, and a single compressed file only needs gzip. Extracting an untrusted archive is dangerous: names with ../ or links can place files outside the target directory, so pass filter='data'. Since 3.14 that filter is the default.

Examples

import tarfile, pathlib
pathlib.Path('a.txt').write_text('hello')
with tarfile.open('arch.tar.gz', 'w:gz') as tar:
    tar.add('a.txt')
with tarfile.open('arch.tar.gz') as tar:
    print(tar.getnames())  # → ['a.txt']
    tar.extractall('out', filter='data')
print(pathlib.Path('out/a.txt').read_text())  # → hello

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy