Skip to content

bpo-44095: Add suffix, stem and suffixes to zipfile.Path #26129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Doc/library/zipfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,27 @@ Path objects are traversable using the ``/`` operator or ``joinpath``.
Return ``True`` if the current context references a file or
directory in the zip file.

.. data:: Path.suffix

The file extension of the final component.

.. versionadded:: 3.11
Added :data:`Path.suffix` property.

.. data:: Path.stem

The final path component, without its suffix.

.. versionadded:: 3.11
Added :data:`Path.stem` property.

.. data:: Path.suffixes

A list of the path’s file extensions.

.. versionadded:: 3.11
Added :data:`Path.suffixes` property.

.. method:: Path.read_text(*, **)

Read the current file as unicode text. Positional and
Expand Down
58 changes: 58 additions & 0 deletions Lib/test/test_zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3093,6 +3093,64 @@ def test_root_name(self, alpharep):
root = zipfile.Path(alpharep)
assert root.name == 'alpharep.zip' == root.filename.name

@pass_alpharep
def test_suffix(self, alpharep):
"""
The suffix of the root should be the suffix of the zipfile.
The suffix of each nested file is the final component's last suffix, if any.
Includes the leading period, just like pathlib.Path.
"""
root = zipfile.Path(alpharep)
assert root.suffix == '.zip' == root.filename.suffix

b = root / "b.txt"
assert b.suffix == ".txt"

c = root / "c" / "filename.tar.gz"
assert c.suffix == ".gz"

d = root / "d"
assert d.suffix == ""

@pass_alpharep
def test_suffixes(self, alpharep):
"""
The suffix of the root should be the suffix of the zipfile.
The suffix of each nested file is the final component's last suffix, if any.
Includes the leading period, just like pathlib.Path.
"""
root = zipfile.Path(alpharep)
assert root.suffixes == ['.zip'] == root.filename.suffixes

b = root / 'b.txt'
assert b.suffixes == ['.txt']

c = root / 'c' / 'filename.tar.gz'
assert c.suffixes == ['.tar', '.gz']

d = root / 'd'
assert d.suffixes == []

e = root / '.hgrc'
assert e.suffixes == []

@pass_alpharep
def test_stem(self, alpharep):
"""
The final path component, without its suffix
"""
root = zipfile.Path(alpharep)
assert root.stem == 'alpharep' == root.filename.stem

b = root / "b.txt"
assert b.stem == "b"

c = root / "c" / "filename.tar.gz"
assert c.stem == "filename.tar"

d = root / "d"
assert d.stem == "d"

@pass_alpharep
def test_root_parent(self, alpharep):
root = zipfile.Path(alpharep)
Expand Down
12 changes: 12 additions & 0 deletions Lib/zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2342,6 +2342,18 @@ def open(self, mode='r', *args, pwd=None, **kwargs):
def name(self):
return pathlib.Path(self.at).name or self.filename.name

@property
def suffix(self):
return pathlib.Path(self.at).suffix or self.filename.suffix

@property
def suffixes(self):
return pathlib.Path(self.at).suffixes or self.filename.suffixes

@property
def stem(self):
return pathlib.Path(self.at).stem or self.filename.stem

@property
def filename(self):
return pathlib.Path(self.root.filename).joinpath(self.at)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:class:`zipfile.Path` now supports :attr:`zipfile.Path.stem`,
:attr:`zipfile.Path.suffixes`, and :attr:`zipfile.Path.suffix` attributes.