Python Glossary Русский

Python 3.12+tarfileexception

tarfile.FilterError

exceptions

The base exception for archive members an extraction filter refused to extract; the member is in the tarinfo attribute.

Syntax

except tarfile.FilterError as e: e.tarinfo

Catch it rather than each subclass: 'path outside', 'absolute link', 'device' are all FilterError. The tarinfo attribute names the offending member, and the message often contains the absolute destination path. With errorlevel=1 (the default) a refusal aborts extractall(), and some files are already on disk — tarfile does not clean them up, so extract into a temporary directory.

Examples

import io, os, tarfile
with tarfile.open('mix.tar', 'w') as tar:
    for name in ('ok.txt', '../bad.txt'):
        info = tarfile.TarInfo(name)
        info.size = 2
        tar.addfile(info, io.BytesIO(b'hi'))
with tarfile.open('mix.tar') as tar:
    try:
        tar.extractall('out', filter='data')
    except tarfile.FilterError as e:
        print(e.tarinfo.name)  # → ../bad.txt
print(os.listdir('out'))  # → ['ok.txt'] — первый файл уже распакован

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy