os.path.join doubt

Chris Rebert clp2 at rebertia.com
Thu Feb 3 01:01:25 EST 2011


On Wed, Feb 2, 2011 at 8:46 PM, harryos <oswald.harry at gmail.com> wrote:
> In windows ,I tried this
>
> p1 = "C:\Users\me\Documents"
> p2 = "..\Pictures\images\my.jpg"
>
> print os.path.join(p1,p2)
> This gives
> 'C:\\Users\\me\\Documents\\..\\Pictures\\images\\my.jpg'
>
> I expected I would get
> 'C:\\Users\\me\\Pictures\\images\\my.jpg'
>
> I thought os.path.join would join the paths more intelligently..Any
> idea why this happens ?

Because that's just not how the function is defined. I admit the
"intelligently" descriptor in its docs is fairly vague.
Call os.path.normpath() on the join() result to simplify the path and
obtain the form you expected.
http://docs.python.org/library/os.path.html#os.path.normpath

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list