On this page
tarfile.open
Prev: tarfile.LinkOutsideDestinationError | Up: tarfile | Next: tarfile.OutsideDestinationError
Rel: tarfile.AbsoluteLinkError, tarfile.AbsolutePathError, tarfile.CompressionError, tarfile.data_filter
Ref: docs.python.org — tarfile.open
Tags: #public #python3 #stdlib #tarfile
Import
import tarfile
from tarfile import open
Signature & Overview
tarfile.open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)
Open a tar archive for reading, writing or appending. Return an appropriate TarFile class.
Usage
import io
import tarfile
buf = io.BytesIO()
with tarfile.open(fileobj=buf, mode="w:gz") as tf:
info = tarfile.TarInfo("hi.txt")
info.size = 2
tf.addfile(info, io.BytesIO(b"ok"))
buf.seek(0)
with tarfile.open(fileobj=buf, mode="r:*") as tf:
assert tf.getnames() == ["hi.txt"]
Gotchas
- Use
"r:*"to auto-detect compression (gz,bz2,xz, or uncompressed), and"r|*"for non-seekable streams/pipes. - Always pass
filter="data"onextractall()(Python 3.12+) when extracting untrusted archives.
REPL Inspect
from tarfile import open
help(open)
Linked from
Related notes
- tuts/python3/stdlib_module/tarfile/LinkOutsideDestinationError Linked here; links to this page; shared tags: #stdlib, #tarfile
- tuts/python3/stdlib_module/tarfile/OutsideDestinationError Linked here; links to this page; shared tags: #stdlib, #tarfile
- tuts/python3/stdlib_module/tarfile/AbsoluteLinkError Linked here; shared tags: #stdlib, #tarfile
- tuts/python3/stdlib_module/tarfile/AbsolutePathError Linked here; shared tags: #stdlib, #tarfile
- tuts/python3/stdlib_module/tarfile/CompressionError Linked here; shared tags: #stdlib, #tarfile
- tuts/python3/stdlib_module/tarfile/data_filter Linked here; shared tags: #stdlib, #tarfile
- tuts/python3/stdlib_module/tarfile/TarFile Linked here; shared tags: #stdlib, #tarfile
- tuts/python3/stdlib_module/tarfile links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tarfile/ExtractError shared tags: #stdlib, #tarfile
- tuts/python3/stdlib_module/tarfile/FilterError shared tags: #stdlib, #tarfile