[issue41565] from os.path import join join('3', '{:3') return '{:3' in windows

Eryk Sun report at bugs.python.org
Tue Aug 18 03:45:06 EDT 2020


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

> I mean,os.path.join('3', '{:3')  return  '{:3' in windows, However, 
> os.path.join('3', '{:3') return  '3/{:3'in linux, output result not 
> '3' in windows, why?

The implementation of ntpath.join handles "{:" as a drive. Thus "{:3" is a drive-relative path, i.e. "3" is relative to the current working directory on drive "{:". 

In particular, for each joined component, ntpath.join calls ntpath.splitdrive, which splits at the second character if it's a colon, regardless of the first character. For example:

    >>> ntpath.splitdrive('{:3')
    ('{:', '3')

As demonstrated in my previous post, the Windows file API supports drive names that use any basic multilingual plane (BMP) character, including symbol characters such as "{". So the long-standing behavior of ntpath.splitdrive in this regard is generally right. Except it shouldn't allow the first character to be a path separator, i.e. the following result is nonsense:

    >>> ntpath.splitdrive('/:/spam')
    ('/:', '/spam')

> with path '{:3' you open file with name '3' in the current 
> directory of drive '{'

The drive name would be "{:", not "{". It's a common usage, for example, to call "C:" the "C" drive, but the drive name actually includes the colon. This differs from the usage of colon in file streams, in which the colon is a delimiter that's not part of the name.

----------

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


More information about the Python-bugs-list mailing list