Skip to content

Commit 46dc4e3

Browse files
sotteberkerpeksag
authored andcommitted
bpo-34329: Doc'd how to remove suffix of pathlib.Path() (GH-8655)
1 parent 5a953fd commit 46dc4e3

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

Doc/library/pathlib.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,14 +559,18 @@ Pure paths provide the following methods and properties:
559559
.. method:: PurePath.with_suffix(suffix)
560560

561561
Return a new path with the :attr:`suffix` changed. If the original path
562-
doesn't have a suffix, the new *suffix* is appended instead::
562+
doesn't have a suffix, the new *suffix* is appended instead. If the
563+
*suffix* is an empty string, the original suffix is removed::
563564

564565
>>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
565566
>>> p.with_suffix('.bz2')
566567
PureWindowsPath('c:/Downloads/pathlib.tar.bz2')
567568
>>> p = PureWindowsPath('README')
568569
>>> p.with_suffix('.txt')
569570
PureWindowsPath('README.txt')
571+
>>> p = PureWindowsPath('README.txt')
572+
>>> p.with_suffix('')
573+
PureWindowsPath('README')
570574

571575

572576
.. _concrete-paths:

Lib/pathlib.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,8 +807,10 @@ def with_name(self, name):
807807
self._parts[:-1] + [name])
808808

809809
def with_suffix(self, suffix):
810-
"""Return a new path with the file suffix changed (or added, if none)."""
811-
# XXX if suffix is None, should the current suffix be removed?
810+
"""Return a new path with the file suffix changed. If the path
811+
has no suffix, add given suffix. If the given suffix is an empty
812+
string, remove the suffix from the path.
813+
"""
812814
f = self._flavour
813815
if f.sep in suffix or f.altsep and f.altsep in suffix:
814816
raise ValueError("Invalid suffix %r" % (suffix))

0 commit comments

Comments
 (0)