Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ba64cc5

Browse filesBrowse 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
Copy full SHA for ba64cc5

File tree

1 file changed

+3
-1
lines changed
Filter options

1 file changed

+3
-1
lines changed

‎Lib/pathlib/types.py

Copy file name to clipboardExpand all lines: Lib/pathlib/types.py
+3-1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.