I would like to change a part of a Path object with pathlib
.
For example if you have a Path object:
import pathlib
path = pathlib.Path("/home/user/to/some/floder/toto.out")
How can I change the file name ? And get a new path with for example "/home/user/to/some/folder/other_file.dat"
?
Or more generally, one can I change one or several elements of that path ?
I can get the parts
of the path:
In [1]: path.parts
Out[1]: ('/', 'home', 'user', 'to', 'some', 'floder', 'toto.out')
Thus, a workaround is to join the needed parts, make a new string and then a new path, but I wonder if there is a more convenient tool to do that.
EDIT
To be more precise, does it exist an equivalent to path.name
that return the complementary part of the path : str(path).replace(path.name, "")
.