Python Glossary Русский

all Python 3tarfileexception

tarfile.ExtractError

exceptions

The tarfile exception for non-fatal extraction errors; it is raised only when errorlevel is 2.

Syntax

except tarfile.ExtractError:

With the default errorlevel=1, non-fatal errors (say, failing to set the owner or file time) only go to the debug log and extraction silently continues — the file appears, but with the wrong attributes. To learn about them, open the archive with errorlevel=2. A custom filter can raise ExtractError to skip a member without aborting the extraction.

Examples

# Python 3.12+
import os, tarfile
for name in ('keep.txt', 'skip.txt'):
    with open(name, 'w', encoding='utf-8') as f:
        f.write(name)
with tarfile.open('x.tar', 'w') as tar:
    tar.add('keep.txt')
    tar.add('skip.txt')
def no_skip(member, path):
    if member.name == 'skip.txt':
        raise tarfile.ExtractError('пропуск')
    return tarfile.data_filter(member, path)
with tarfile.open('x.tar') as tar:
    tar.extractall('out', filter=no_skip)
print(sorted(os.listdir('out')))  # → ['keep.txt'] — при errorlevel=1 молча пропущен
with tarfile.open('x.tar', errorlevel=2) as tar:
    try:
        tar.extractall('out2', filter=no_skip)
    except tarfile.ExtractError as e:
        print(e)  # → пропуск

See also

Open in the full glossarydocs.python.orgReport an inaccuracy