On this page
shutil.unpack_archive
Prev: shutil.SpecialFileError | Up: shutil | Next: shutil.unregister_archive_format
Rel: shutil.chown, shutil.copy, shutil.copy2, shutil.copyfile
Ref: docs.python.org — shutil.unpack_archive
Tags: #public #python3 #stdlib #shutil
Import
import shutil
from shutil import unpack_archive
Signature & Overview
shutil.unpack_archive(filename, extract_dir=None, format=None, *, filter=None)
Unpack an archive.
Usage
from pathlib import Path
import shutil
import tempfile
with tempfile.TemporaryDirectory() as tmp:
src = Path(tmp) / "src"
src.mkdir()
(src / "hello.txt").write_text("world", encoding="utf-8")
archive = shutil.make_archive(str(Path(tmp) / "bundle"), "zip", root_dir=src)
out = Path(tmp) / "extracted"
shutil.unpack_archive(archive, out)
assert (out / "hello.txt").read_text(encoding="utf-8") == "world"
Gotchas
- Watch for path-traversal ("Zip Slip") on untrusted archives: For tar archives on Python 3.12+, pass
filter="data"to block escaping paths, symlinks, and special files. - Format auto-detected from extension: Pass
format="zip"explicitly if the filename lacks a standard suffix.
REPL Inspect
from shutil import unpack_archive
help(unpack_archive)
Linked from
Related notes
- tuts/python3/stdlib_module/shutil/SpecialFileError Linked here; links to this page; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/unregister_archive_format Linked here; links to this page; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/shutil/chown Linked here; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copy Linked here; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copy2 Linked here; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copyfile Linked here; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copyfileobj shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copymode shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copystat shared tags: #shutil, #stdlib