How to make a new Path object from parts of a current path with pathlib?

In order to sum up the comments, the statements are the following:

  1. In order to change the file name
In [1]: import pathlib

In [2]: path = pathlib.Path("/home/user/to/some/folder/toto.out")

In [3]: path.parent / "other_file.dat"
Out[3]: PosixPath('/home/user/to/some/folder/other_file.dat')

  1. In order to change one part in the path
In [4]: parts = list(path.parts)

In [5]: parts[4] = "other"

In [6]: pathlib.Path(*parts)
Out[6]: PosixPath('/home/user/to/other/folder/toto.out')

Leave a Comment

tech