From be11d5e7227d05e140b8c77e9befd323780498b1 Mon Sep 17 00:00:00 2001 From: Stefan Otte Date: Fri, 3 Aug 2018 11:35:57 +0200 Subject: [PATCH 1/3] Document how to remove a suffix of a pathlib.Path --- Doc/library/pathlib.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst index 3d28a1bea30315e..8f8a7580ed263be 100644 --- a/Doc/library/pathlib.rst +++ b/Doc/library/pathlib.rst @@ -559,7 +559,8 @@ Pure paths provide the following methods and properties: .. method:: PurePath.with_suffix(suffix) 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.with_suffix('.bz2') @@ -567,6 +568,9 @@ Pure paths provide the following methods and properties: >>> p = PureWindowsPath('README') >>> p.with_suffix('.txt') PureWindowsPath('README.txt') + >>> p = PureWindowsPath('README.txt') + >>> p.with_suffix('') + PureWindowsPath('README') .. _concrete-paths: From 546aba60a74d9d9cf8c4a986da10c7009f80bf6b Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Fri, 3 Aug 2018 17:26:25 +0300 Subject: [PATCH 2/3] tweak wording a bit --- Doc/library/pathlib.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst index 8f8a7580ed263be..0929ee27c19e164 100644 --- a/Doc/library/pathlib.rst +++ b/Doc/library/pathlib.rst @@ -559,8 +559,8 @@ Pure paths provide the following methods and properties: .. method:: PurePath.with_suffix(suffix) Return a new path with the :attr:`suffix` changed. If the original path - doesn't have a suffix, the new *suffix* is appended instead. If the - *suffix* is an empty string the original suffix is removed:: + 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.with_suffix('.bz2') From 7a8d530c8dc26bf20cd9c7a74181f2a3728e0348 Mon Sep 17 00:00:00 2001 From: Stefan Otte Date: Fri, 3 Aug 2018 17:32:23 +0200 Subject: [PATCH 3/3] Improve docstring and rm obsolete comment --- Lib/pathlib.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 8431c29c1d6516f..4fe9d4aefc3b0ce 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -807,8 +807,10 @@ def with_name(self, name): self._parts[:-1] + [name]) def with_suffix(self, suffix): - """Return a new path with the file suffix changed (or added, if none).""" - # XXX if suffix is None, should the current suffix be removed? + """Return a new path with the file suffix changed. If the path + has no suffix, add given suffix. If the given suffix is an empty + string, remove the suffix from the path. + """ f = self._flavour if f.sep in suffix or f.altsep and f.altsep in suffix: raise ValueError("Invalid suffix %r" % (suffix))