Python Glossary Русский

all Python 3tarfileexception

tarfile.StreamError

exceptions

The tarfile exception for a stream archive (a mode with '|') being asked for an operation that needs seeking backwards.

Syntax

except tarfile.StreamError:

Modes 'r|gz' and 'w|gz' read and write strictly forward, so they work with a socket or stdin. But after getmembers() you cannot go back to a member already passed: extractfile() raises StreamError 'seeking backwards is not allowed'. In a stream, process members one by one in a for member in tar loop, reading each right away.

Examples

import tarfile
with open('a.txt', 'w', encoding='utf-8') as f:
    f.write('data')
with tarfile.open('s.tar', 'w') as tar:
    tar.add('a.txt')
with tarfile.open('s.tar', 'r|') as tar:
    first = tar.getmembers()[0]
    try:
        tar.extractfile(first).read()
    except tarfile.StreamError as e:
        print(e)  # → seeking backwards is not allowed

See also

Open in the full glossarydocs.python.orgReport an inaccuracy