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."""