Python Glossary Русский

all Python 3tarfilefunction

tarfile.is_tarfile()

tar archives

Checks whether the tarfile module can open a file as an archive; it accepts a path or a file object.

Syntax

tarfile.is_tarfile(name)

The check is shallow: it is enough for the first header to parse, and a file of nothing but zero bytes counts as an empty tar archive — is_tarfile returns True. It guarantees neither that extraction will succeed nor that the contents are safe. Compressed .tar.gz/.tar.bz2/.tar.xz are recognised automatically. File objects are supported since Python 3.9.

Examples

import tarfile
with open('zeros.bin', 'wb') as f:
    f.write(b'\0' * 1024)
with open('text.txt', 'w', encoding='utf-8') as f:
    f.write('hello')
print(tarfile.is_tarfile('zeros.bin'))  # → True — нули считаются пустым архивом
print(tarfile.is_tarfile('text.txt'))  # → False

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy