Behaviour of os.path.join

DL Neil PythonList at DancesWithMice.info
Wed May 27 18:14:02 EDT 2020


...

>> Again, a pathname is never inherently a directory or a file.

See previous contribution: until YOU define YOUR arena of operations, it 
will be difficult to select the correct tool or library - or for others 
to assist you.

If dealing with strings (which happen to look as if they are 
file/directory names) then use str.join() plus build custom functions to 
suit yourself/your specification.

If dealing with strings which represent files/directories, use pathlib's 
abstraction.

If the above applies, but eventually are applied to a file-system, use 
pathlib's "concrete" classes, at that point.

If you are prepared to accept the os/os.path library's abstraction(s), 
then please go-ahead with that alternative.

If any of the provided libraries are insufficient, please search Pypi 
(etc) for a suitable alternative, or develop your own routines.


> You can define a path however you want but it won't change the fact that
> on Windows a path that ends in '\\' is inherently a path to a directory.

Referring to the first clause:
again, see previous contribution, and link to descriptions of paths 
(etc) and comparisons across OpSys. We can't define/re-define such terms 
to suit ourselves - nor does Python.

If your specification embraces only MS-Windows, then please ignore the 
Python facilities which attempt a more universal solution. If, however, 
you choose a more universal abstraction, then please accept that it will 
not (necessarily) offer solutions which are Windows-specific - by 
definition.


Referring to the final conclusion:
a solution already exists (again, please refer to docs/link in previous 
contribution, and repeated 'here' by @Beverley). She provides evidence 
for macOS, here is same from Linux:

 >>> os.path.join( '/dir', 'dir', '' )
'/dir/dir/'

Personally, I find the final path (as empty string) to be both ugly and 
a little confusing, but by ignoring the inherent 'intelligence' we can 
direct:

 >>> os.path.join( '/dir', 'dir/' )
'/dir/dir/'

What happens on MS-Windows?


Of course, given that this -join() doesn't care about content, but 
requires only "path(s)" as parameter(s), we can 'do our own thing':

 >>> os.path.join( '/dir', 'file' )
'/dir/file'
 >>> os.path.join( '/dir', 'file/' )
'/dir/file/'

???
With great power, comes great...
- and from this conversation it would appear many (like me) wouldn't 
want to contemplate such a thing. However, it *is* possible, and given 
that it satisfies your stated spec, you could "define...however you 
want" and make the decision without reference to 'the rest of us'.


Once you have coded your solution, which you could then use as 'sample 
code', please feel free to suggest a bug-fix which describes:-
- the problem-set,
- the scenario(s) in which it is a problem,
- the problems of your solution (as coded), and
- your idea for a more elegant, Pythonic, solution.
-- 
Regards =dn


More information about the Python-list mailing list