Commit 46dc4e34 authored by Stefan Otte's avatar Stefan Otte Committed by Berker Peksag

bpo-34329: Doc'd how to remove suffix of pathlib.Path() (GH-8655)

parent 5a953fd0
...@@ -559,7 +559,8 @@ Pure paths provide the following methods and properties: ...@@ -559,7 +559,8 @@ Pure paths provide the following methods and properties:
.. method:: PurePath.with_suffix(suffix) .. method:: PurePath.with_suffix(suffix)
Return a new path with the :attr:`suffix` changed. If the original path Return a new path with the :attr:`suffix` changed. If the original path
doesn't have a suffix, the new *suffix* is appended instead:: doesn't have a suffix, the new *suffix* is appended instead. If the
*suffix* is an empty string, the original suffix is removed::
>>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz') >>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
>>> p.with_suffix('.bz2') >>> p.with_suffix('.bz2')
...@@ -567,6 +568,9 @@ Pure paths provide the following methods and properties: ...@@ -567,6 +568,9 @@ Pure paths provide the following methods and properties:
>>> p = PureWindowsPath('README') >>> p = PureWindowsPath('README')
>>> p.with_suffix('.txt') >>> p.with_suffix('.txt')
PureWindowsPath('README.txt') PureWindowsPath('README.txt')
>>> p = PureWindowsPath('README.txt')
>>> p.with_suffix('')
PureWindowsPath('README')
.. _concrete-paths: .. _concrete-paths:
......
...@@ -807,8 +807,10 @@ class PurePath(object): ...@@ -807,8 +807,10 @@ class PurePath(object):
self._parts[:-1] + [name]) self._parts[:-1] + [name])
def with_suffix(self, suffix): def with_suffix(self, suffix):
"""Return a new path with the file suffix changed (or added, if none).""" """Return a new path with the file suffix changed. If the path
# XXX if suffix is None, should the current suffix be removed? has no suffix, add given suffix. If the given suffix is an empty
string, remove the suffix from the path.
"""
f = self._flavour f = self._flavour
if f.sep in suffix or f.altsep and f.altsep in suffix: if f.sep in suffix or f.altsep and f.altsep in suffix:
raise ValueError("Invalid suffix %r" % (suffix)) raise ValueError("Invalid suffix %r" % (suffix))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment