On this page
tarfile.is_tarfile
Prev: tarfile.HeaderError | Up: tarfile | Next: tarfile.LinkFallbackError
Rel: tarfile.AbsoluteLinkError, tarfile.AbsolutePathError, tarfile.CompressionError, tarfile.data_filter
Ref: docs.python.org — tarfile.is_tarfile
Tags: #public #python3 #stdlib #tarfile
Import
import tarfile
from tarfile import is_tarfile
Signature & Overview
tarfile.is_tarfile(name)
Return True if name points to a tar archive that we are able to handle, else return False.
Usage
import io
import tarfile
buf = io.BytesIO()
with tarfile.open(fileobj=buf, mode="w:gz") as tf:
info = tarfile.TarInfo("a.txt")
tf.addfile(info, io.BytesIO(b""))
buf.seek(0)
assert tarfile.is_tarfile(buf) is True
assert tarfile.is_tarfile(io.BytesIO(b"plain")) is False
Gotchas
- Rewinds/reads the stream: Remember to
buf.seek(0)before opening aBytesIOstream withtarfile.open()afterward.
REPL Inspect
from tarfile import is_tarfile
help(is_tarfile)
Linked from
Related notes
- tuts/python3/stdlib_module/tarfile/HeaderError Linked here; links to this page; shared tags: #stdlib, #tarfile
- tuts/python3/stdlib_module/tarfile/LinkFallbackError 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