I just spent fifteen minutes debugging a problem in my code, it boils down to the fact that when you concate nate two paths, if the latter has a leading slash, the concatenation is not done and the latter path is taken, i.e.
>>>
>>> from pathlib import Path
>>>
>>> a = Path('/x/y/z')
>>> b = Path('p/q/r')
>>>
>>> a / b
PosixPath('/x/y/z/p/q/r')
>>>
>>>
>>> b = Path('/p/q/r')
>>>
>>> a / b
PosixPath('/p/q/r')
>>>
I think this should not happen and the path should just eliminate the leading slash in the next path, or at least I would rais an exception.
In Windows that b is not absolute. If you try something like r'C:\p\q\r', it should keep b.
Another case to take into account is a being a PosixPath and b being an absolute PureWindowsPath. In that case a/b also doesnât just keep b. I think because it first interprets b as a path of the same type as a, and there it is not absolute.
/ is the root on Posix. Shouldnât appending /foo to another path error, or does Windows need to do that for some cursed reason?
Regardless, eliminating leading slashes is a bad idea - thatâs not a redundant os.sep, thatâs a crucial piece of information.
Path.joinpath behaves the same. Iâm not suggesting a breaking change should be made.
Is there demand for a safe method (or optional args to joinpath) that doesnât discard the leading slash and errors? Or for a way to append a root path ignoring its root, always producing a child path? I realise Iâm interchanging path to mean both pathlib.Path and âstring of a posix file pathâ.
Everything to do with Windows paths is cursed, so, yes, it is. The path "C:/spam" is absolute; the path "C:spam" is not, and the path "/spam" is not. Or rather, theyâre partly absolute. Awesome, isnât it?
Iâve been bitten by Windows Paths being case insensitive before, and keeping their original name even when renamed to switch the case of a few characters.