Search code examples
python-3.xfilepathpathlib

pathlib joinpath in pandas 3.7


I am trying to join a relative path to an absolute one. I am confused as to this behavior:

from pathlib import Path
path = Path("/an/absolute/path/test")
path.joinpath("/../relative/path", "some_suffixes")

gives

PosixPath('/../relative/path/some_suffixes')

Why does this drop the first part of the path? What I expect is

PosixPath('/an/absolute/path/test/../relative/path/some_suffixes')

Solution

  • Seems like your problem is the relative path string you provide. You should remove the front forward slash and you should be good to go.

    path.joinpath("../relative/path", "some_suffixes")