Behaviour of os.path.join

Andrew Jaffe a.h.jaffe at gmail.com
Wed May 27 11:46:55 EDT 2020


Dear all,

\On 26/05/2020 15:56, 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!
> 
> Moreover, if I use
> 
>    join(base, '..\\..\\', 'build', os.sep)
> 
> I get the same result.
> 
> This seems to me to be a bug that I should report but to avoid wasting
> developer time I wanted to hear what others feel about this.

Maybe I am being obtuse (and apologies for not quoting any of the 
subsequent voluminous messages in this thread), but I think perhaps 
there is confusion at a somewhat more basic level.

It seems that there is never (rarely?) any reason to explicitly pass a 
string which already contains an explicit separator to `os.path.join` -- 
the whole point of the function is to be os-agnostic.

If you already know what the separator is, and you also don't like the 
behaviour of restarting the path if any of the items are (by the 
function's definition) absolute, then is there any reason to prefer 
`os.path.join` to `string.join`?

A





More information about the Python-list mailing list