diff options
Diffstat (limited to 'Lib/pathlib/__init__.py')
-rw-r--r-- | Lib/pathlib/__init__.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/Lib/pathlib/__init__.py b/Lib/pathlib/__init__.py index 115ccf7..765a142 100644 --- a/Lib/pathlib/__init__.py +++ b/Lib/pathlib/__init__.py @@ -166,6 +166,25 @@ class PurePath(_abc.PurePathBase): return NotImplemented return self._parts_normcase >= other._parts_normcase + @property + def name(self): + """The final path component, if any.""" + tail = self._tail + if not tail: + return '' + return tail[-1] + + def with_name(self, name): + """Return a new path with the file name changed.""" + m = self.pathmod + if not name or m.sep in name or (m.altsep and m.altsep in name) or name == '.': + raise ValueError(f"Invalid name {name!r}") + tail = self._tail.copy() + if not tail: + raise ValueError(f"{self!r} has an empty name") + tail[-1] = name + return self._from_parsed_parts(self.drive, self.root, tail) + def relative_to(self, other, /, *_deprecated, walk_up=False): """Return the relative path to another path identified by the passed arguments. If the operation is not possible (because this is not |