10000 bpo-44095: Add suffix, stem and suffixes to zipfile.Path (GH-26129) · python/cpython@dc0b364 · GitHub
[go: up one dir, main page]

Skip to content

Commit dc0b364

Browse files
authored
bpo-44095: Add suffix, stem and suffixes to zipfile.Path (GH-26129)
1 parent 2918846 commit dc0b364

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

Doc/library/zipfile.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,27 @@ Path objects are traversable using the ``/`` operator or ``joinpath``.
521521
Return ``True`` if the current context references a file or
522522
directory in the zip file.
523523

524+
.. data:: Path.suffix
525+
526+
The file extension of the final component.
527+
528+
.. versionadded:: 3.11
529+
Added :data:`Path.suffix` property.
530+
531+
.. data:: Path.stem
532+
< 10000 /td>533+
The final path component, without its suffix.
534+
535+
.. versionadded:: 3.11
536+
Added :data:`Path.stem` property.
537+
538+
.. data:: Path.suffixes
539+
540+
A list of the path’s file extensions.
541+
542+
.. versionadded:: 3.11
543+
Added :data:`Path.suffixes` property.
544+
524545
.. method:: Path.read_text(*, **)
525546

526547
Read the current file as unicode text. Positional and

Lib/test/test_zipfile.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3093,6 +3093,64 @@ def test_root_name(self, alpharep):
30933093
root = zipfile.Path(alpharep)
30943094
assert root.name == 'alpharep.zip' == root.filename.name
30953095

3096+
@pass_alpharep
3097+
def test_suffix(self, alpharep):
3098+
"""
3099+
The suffix of the root should be the suffix of the zipfile.
3100+
The suffix of each nested file is the final component's last suffix, if any.
3101+
Includes the leading period, just like pathlib.Path.
3102+
"""
3103+
root = zipfile.Path(alpharep)
3104+
assert root.suffix == '.zip' == root.filename.suffix
3105+
3106+
b = root / "b.txt"
3107+
assert b.suffix == ".txt"
3108+
3109+
c = root / "c" / "filename.tar.gz"
3110+
assert c.suffix == ".gz"
3111+
3112+
d = root / "d"
3113+
assert d.suffix == ""
3114+
3115+
@pass_alpharep
3116+
def test_suffixes(self, alpharep):
3117+
"""
3118+
The suffix of the root should be the suffix of the zipfile.
3119+
The suffix of each nested file is the final component's last suffix, if any.
3120+
Includes the leading period, just like pathlib.Path.
3121+
"""
3122+
root = zipfile.Path(alpharep)
3123+
assert root.suffixes == ['.zip'] == root.filename.suffixes
3124+
3125+
b = root / 'b.txt'
3126+
assert b.suffixes == ['.txt']
3127+
3128+
c = root / 'c' / 'filename.tar.gz'
3129+
assert c.suffixes == ['.tar', '.gz']
3130+
3131+
d = root / 'd'
3132+
assert d.suffixes == []
3133+
3134+
e = root / '.hgrc'
3135+
assert e.suffixes == []
3136+
3137+
@pass_alpharep
3138+< 8000 /span>
def test_stem(self, alpharep):
3139+
"""
3140+
The final path component, without its suffix
3141+
"""
3142+
root = zipfile.Path(alpharep)
3143+
assert root.stem == 'alpharep' == root.filename.stem
3144+
3145+
b = root / "b.txt"
3146+
assert b.stem == "b"
3147+
3148+
c = root / "c" / "filename.tar.gz"
3149+
assert c.stem == "filename.tar"
3150+
3151+
d = root / "d"
3152+
assert d.stem == "d"
3153+
30963154
@pass_alpharep
30973155
def test_root_parent(self, alpharep):
30983156
root = zipfile.Path(alpharep)

Lib/zipfile.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,6 +2342,18 @@ def open(self, mode='r', *args, pwd=None, **kwargs):
23422342
def name(self):
23432343
return pathlib.Path(self.at).name or self.filename.name
23442344

2345+
@property
2346+
def suffix(self):
2347+
return pathlib.Path(self.at).suffix or self.filename.suffix
2348+
2349+
@property
2350+
def suffixes(self):
2351+
return pathlib.Path(self.at).suffixes or self.filename.suffixes
2352+
2353+
@property
2354+
def stem(self):
2355+
return pathlib.Path(self.at).stem or self.filename.stem
2356+
23452357
@property
23462358
def filename(self):
23472359
return pathlib.Path(self.root.filename).joinpath(self.at)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:class:`zipfile.Path` now supports :attr:`zipfile.Path.stem`,
2+
:attr:`zipfile.Path.suffixes`, and :attr:`zipfile.Path.suffix` attributes.

0 commit comments

Comments
 (0)
0