On this page
  1. tarfile.open
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

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

REPL Inspect

from tarfile import open
help(open)

Linked from

Related notes