Skip to content

Commit ba64cc5

Browse files
authored
GH-130614: pathlib ABCs: retain original separator in with_name() (#130990)
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.
1 parent c240c2d commit ba64cc5

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

Lib/pathlib/types.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ def with_name(self, name):
135135
split = self.parser.split
136136
if split(name)[0]:
137137
raise ValueError(f"Invalid name {name!r}")
138-
return self.with_segments(split(str(self))[0], name)
138+
path = str(self)
139+
path = path.removesuffix(split(path)[1]) + name
140+
return self.with_segments(path)
139141

140142
def with_stem(self, stem):
141143
"""Return a new path with the stem changed."""

0 commit comments

Comments
 (0)