Behaviour of os.path.join

Eryk Sun eryksun at gmail.com
Thu May 28 09:19:10 EDT 2020


On 5/26/20, BlindAnagram <blindanagram at nowhere.com> wrote:
>
> But if I try to make the directory myself (as I tried first):
>
>   join(base, '..\\..\\', 'build', '\\')
>
> I obtain:
>
> 'C:\\'
>
> The documentation says that an absolute path in the parameter list for
> join will discard all previous parameters but '\\' is not an absoute path!

First, to be clear, a rooted path in Windows is not absolute (*), but
that's not relevant here. What happens is that joining a path is like
issuing successive "cd" commands in the shell, and "cd \" takes you to
the root path of the current drive. The implementation of
ntpath.join() similarly tracks the current drive via
ntpath.splitdrive() while joining components.

---

(*)
When opened, a rooted path gets resolved relative to the drive of the
process working directory. Except in a relative symlink, where a
rooted target path is relative to the device of the parsed path of the
symlink. For example, if the parsed, native NT path of a symlink is
r"\Device\HarddiskVolume10\path\to\symlink", and the symlink targets
r"\path\to\file", then it gets resolved as
r"\Device\HarddiskVolume10\path\to\file".

ntpath.isabs incorrectly classifies rooted paths as absolute, and its
use is actually breaking part of the fallback code in the new
implementation of ntpath.realpath when manually resolving relative
symlinks. I've known of the problem for a few years and have discussed
it one one or two issues on the tracker, but correcting the behavior
of such a commonly used function is not an easy change to propose.
Usually mistakes made early on in development become permanent warts.


More information about the Python-list mailing list