On this page
zipfile.Path
Prev: zipfile.LargeZipFile | Up: zipfile | Next: zipfile.PyZipFile
Rel: zipfile.BadZipFile, zipfile.BadZipfile, zipfile.error, zipfile.is_zipfile
Ref: docs.python.org — zipfile.Path
Tags: #public #python3 #stdlib #zipfile
Import
import zipfile
from zipfile import Path
Signature & Overview
zipfile.Path(root, at='')
A :class:importlib.resources.abc.Traversable interface for zip files.
Methods & Attributes
Path.exists(self)—Path.existsmethod/property.Path.filename(...)—Path.filenamemethod/property.Path.glob(self, pattern)—Path.globmethod/property.Path.is_dir(self)—Path.is_dirmethod/property.Path.is_file(self)—Path.is_filemethod/property.Path.is_symlink(self)— Return whether this path is a symlink.Path.iterdir(self)—Path.iterdirmethod/property.Path.joinpath(self, *other)—Path.joinpathmethod/property.Path.match(self, path_pattern)—Path.matchmethod/property.Path.name(...)—Path.namemethod/property.Path.open(self, mode='r', *args, pwd=None, **kwargs)— Open this entry as text or binary following the semantics ofpathlib.Path.open()by passing arguments through to io.TextIOWrapper().Path.parent(...)—Path.parentmethod/property.Path.read_bytes(self)—Path.read_bytesmethod/property.Path.read_text(self, *args, **kwargs)—Path.read_textmethod/property.Path.relative_to(self, other, *extra)—Path.relative_tomethod/property.Path.rglob(self, pattern)—Path.rglobmethod/property.Path.stem(...)—Path.stemmethod/property.Path.suffix(...)—Path.suffixmethod/property.Path.suffixes(...)—Path.suffixesmethod/property.
Usage
import io
import zipfile
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w") as zf:
zf.writestr("docs/readme.txt", "hello zip path")
buf.seek(0)
with zipfile.ZipFile(buf, "r") as zf:
root = zipfile.Path(zf)
assert (root / "docs" / "readme.txt").read_text(encoding="utf-8") == "hello zip path"
Gotchas
pathlib-like API over zip members: Lets you traverse subdirectories with/and.iterdir()without extracting the archive to disk.
REPL Inspect
from zipfile import Path
help(Path)
Linked from
Related notes
- tuts/python3/stdlib_module/zipfile/LargeZipFile Linked here; links to this page; shared tags: #stdlib, #zipfile
- tuts/python3/stdlib_module/zipfile/PyZipFile Linked here; links to this page; shared tags: #stdlib, #zipfile
- tuts/python3/stdlib_module/zipfile/BadZipFile Linked here; shared tags: #stdlib, #zipfile
- tuts/python3/stdlib_module/zipfile/BadZipfile_class Linked here; shared tags: #stdlib, #zipfile
- tuts/python3/stdlib_module/zipfile/error Linked here; shared tags: #stdlib, #zipfile
- tuts/python3/stdlib_module/zipfile/is_zipfile Linked here; shared tags: #stdlib, #zipfile
- tuts/python3/stdlib_module/zipfile/ZipFile Linked here; shared tags: #stdlib, #zipfile
- tuts/python3/stdlib_module/zipfile links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zipfile/ZipInfo shared tags: #stdlib, #zipfile
- tuts/c/stdlib shared tags: #stdlib