From c5a9b8deae8ae2f5c08abbd4da42a9599ab8cf5c Mon Sep 17 00:00:00 2001 From: barneygale Date: Sat, 8 Mar 2025 23:15:47 +0000 Subject: [PATCH] GH-130614: pathlib ABCs: retain original separator in `with_name()` In `pathlib.types._JoinablePath.with_name()`, retain any alternative path separator preceding the old name, rather stripping and replacing it with a primary separator. As a result, this method changes _only_ the name. --- Lib/pathlib/types.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/pathlib/types.py b/Lib/pathlib/types.py index cfdbc89644a24a..c65e4e3ede90b1 100644 --- a/Lib/pathlib/types.py +++ b/Lib/pathlib/types.py @@ -135,7 +135,9 @@ def with_name(self, name): split = self.parser.split if split(name)[0]: raise ValueError(f"Invalid name {name!r}") - return self.with_segments(split(str(self))[0], name) + path = str(self) + path = path.removesuffix(split(path)[1]) + name + return self.with_segments(path) def with_stem(self, stem): """Return a new path with the stem changed."""