[Python-ideas] os.path.join() failure

Jacob Holm jh at improva.dk
Tue Oct 2 15:36:53 CEST 2007


Aahz wrote:
> On Tue, Oct 02, 2007, Greg Ewing wrote:
>   
>> Steven Bethard wrote:
>>     
>>> I write quite a few Windows paths, and probably make the
>>> final-backslash mistake once a week.
>>>       
>> If you were using os.path.join(), as you should be, you wouldn't ever
>> have to write a trailing backslash in a path in the first place. Or
>> any backslashes at all, for that matter.
>>     
>
> Wrong.  And I just got bitten by this yesterday (no backslashes at all).
> Consider the difference between
>
> cp -a foo bar
>
> and
>
> cp -a foo/ bar
>
> The two are almost identical *except* when foo is a symlink to a
> directory, then the first form copies the symlink instead of giving a
> brand-new directory.  Which, since foo/ is a template dir that gets
> modified after copying was a very bad thing.  (We moved servers around
> over the weekend and for convenience's sake made foo a symlink to a new
> location.)
>
> And yes, we *were* using os.path.join().  So trailing slashes (or
> backslashes) are in fact sometimes required.
>   
And that is easily doable using os.path.join. Just use an empty string 
as the last argument.

 >>> import os.path
 >>> os.path.join('foo','')
foo/

So no, you do *not* need to write trailing slashes.

-- 
Jacob Holm
CTO
Improva ApS




More information about the Python-ideas mailing list