Behaviour of os.path.join

Roel Schroeven roel at roelschroeven.net
Thu May 28 14:39:28 EDT 2020


BlindAnagram schreef op 28/05/2020 om 11:00:
> On 27/05/2020 23:39, Roel Schroeven wrote: 
>> I find no hints of adding a backslash at the end to indicate directories.
>> If you can point me to convincing evidence in the documentation I'll
>> change my mind.
> 
> And if you find a counterexample, I will change mine.

It's hard to prove a negative.

The only thing I can find is that there are cases where doing operations 
on files fail if there's a backslash at the end, whereas operations on 
directories do work with a backslash at the end; but those also work 
without the backslash. Even then, the distinction between what operation 
is done is never decided by the backslash; only if it is decided that 
you're doing something file-related and there is a backslash, then it 
fails. It's not like it turns into a directory-related operation. Ergo, 
adding backslashes practically accomplishes nothing.

What really defines what kind of operation will be done, is
- which function you call
- or the flags you pass to the function
- or for some operation when working with existing objects, the type of 
those existing objects

Also, if we're going to look in the other direction: we can ask Windows 
for information about objects on disk, and it will tell use the type 
using flags, not by adding backslashes. We can use e.g. FindFirstFile / 
FindNextFile / FindClose. dwFileAttributes in the WIN32_FIND_DATA 
structure will have FILE_ATTRIBUTE_DIRECTORY set for directories and not 
for normal files. cFileName won't have a trailing backslash. If the 
semantics were as you say, I'd expect Windows to at least using them itself.

In summary, IMO:

- Windows allows trailing backslashes for directories.
- Windows doesn't allow trailing backslashes for files.
- Windows doesn't use trailing backslashes for distinction between files 
and directories.
- And so I don't see how/why adding a backslash makes any real difference.

-- 
"Honest criticism is hard to take, particularly from a relative, a
friend, an acquaintance, or a stranger."
         -- Franklin P. Jones

Roel Schroeven



More information about the Python-list mailing list