0

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')
michel
  • 282
  • 2
  • 17
  • I think you meant to do `path.joinpath(path, "some_suffixes")` – Onyambu May 21 '20 at 23:43
  • When encountering an absolute path, path-joining function like `os.path.join` and `Path.joinpath` use that absolute path as the starting point. This is logically correct. – Aaron May 22 '20 at 00:00

1 Answers1

1

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")
stavrosfil
  • 36
  • 3