Behaviour of os.path.join

BlindAnagram blindanagram at nowhere.com
Tue May 26 12:57:54 EDT 2020


On 26/05/2020 16:59, Mats Wichmann wrote:
> On 5/26/20 8:56 AM, BlindAnagram wrote:
>> I came across an issue that I am wondering whether I should report as an
>> issue.  If I have a directory, say:
>>
>>   base='C:\\Documents'
>>
>> and I use os.path.join() as follows:
>>
>>   join(base, '..\\..\\', 'build', '')
>>
>> I obtain as expected from the documentation:
>>
>> 'C:\\Documents\\..\\..\\build\\'
>>
>> 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!
> 
> But it is - an absolute path is one that starts with the pathname separator.

On Windows, when part of a string representing a file or directory path,
'\\' acts as a directory separator.  It may imply, as you say, that it
is a path on the current drive but it also behaves as a move to a
sub-directory of what has appeared to its left.  And this is how it is
treated in other os.path  functions. For example:

base = "C:\\Documents\\Finance\\"
abspath(base)
'C:\\Documents\\Finance'

where it can be seen that '\\' is being treated as a directory
separator.  In my view it is not sensible to have some functions
treating '\\' in one way and others differently.  I would hence expect
join() to accept '\\'  as an addition to a file path on Windows and not
a signal of an absolute path.

> The concept of paths is ugly in Windows because of the drive letter - a
> drive letter is not actually part of a path, it's an additional piece of
> context.  If you leave out the drive letter, your path is relative or
> absolute within the current drive letter; if you include it your path is
> relative or absolute within the specified drive letter.  So Python has
> behaved as documented here: the indicator for an absolute path has
> discarded everything (except the drive letter, which is necessary to
> maintain the context you provided) which came before it in the join.
> 
> If indeed you're seeking a path that is terminated by the separator
> character, you need to do what you did in the first example - join an
> empty string at the end (this is documented).  The terminating separator
> _usually_ isn't needed.  Sadly, sometimes it appears to be...
I agree that handling file paths on Windows is very ugly, especially so
if some of the directory/file names include spaces.  But I don't see
that as an excuse for this specific ambiguity in Python in the  handling
of '\\' when embedded in, or added to, paths.



More information about the Python-list mailing list