[issue44452] Allow paths to be joined without worrying about a leading slash

Eryk Sun report at bugs.python.org
Fri Nov 26 16:02:59 EST 2021


Eryk Sun <eryksun at gmail.com> added the comment:

> and b / a gives me WindowsPath('/a/b'). So I'm like "ok, a seems
> like absolute, I will test for that" but on Windows a.is_absolute()
> is False.

Path.is_absolute() is true if a path has a `root` and, for a Windows path, also a `drive`.

In C++, the filesystem::path is_absolute() method is similar. In Windows it's true if both has_root_name() (i.e. a drive) and has_root_directory() are true. In POSIX it just depends on has_root_directory(). pathlib.Path is also consistent with C++ filesystem::path with regard to appending paths with the slash operator [1]. I would prefer for it to remain so.

FYI, in Windows, "/a/b" is resolved using the drive of the process current working directory, which may be a UNC path. The drive of a UNC path is the share path, such as "\\server\share". 

Another type of relative path in Windows is a drive-relative path, which applies to drive-letter drives only. For example:

    >>> Path('C:spam') / "eggs"
    WindowsPath('C:spam/eggs')
    >>> Path('C:spam') / "/eggs"
    WindowsPath('C:/eggs')

"C:spam" is relative to the current working directory on drive "C:". The API gets the working directory for the target drive either from the process current working directory, if it's a path on the target drive, or from an "=<drive letter>:" environment variable, such as "=Z:". (The Windows API allows environment variable names to begin with "=".) If the process current working directory is on a different drive, and the environment variable for the target drive isn't set, the API defaults to using the root directory on the target drive. Setting these per-drive working directory environment variables is up to the application. Python's os.chdir() supports them. 

---
[1] https://en.cppreference.com/w/cpp/filesystem/path/append

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44452>
_______________________________________


More information about the Python-bugs-list mailing list