os.path.normpath()

Steve Holden sholden at holdenweb.com
Wed Jan 24 17:47:09 EST 2001


"Gawain Bolton" <gbolton at wanadoo.fr> wrote in message
news:3A6F4777.60A82F5B at wanadoo.fr...
> I'm using Python v1.5.2 on Sun and Linux and have noticed what, to me,
> is strange behaviour with the os.path.normpath() function.
>
> Namely, I noticed the following:
>
> 1. os.path.normpath('///A///B///C///D') gives '///A/B/C/D'
>
I don't know why this would be ... although it *is* a valid, path, of
course. I presume you'd like just one leading slash on absolute paths.

> and
>
> 2. os.path.normpath('/../A/../A/B') gives '/../A/B'
>
> I'm sure I'm not the first one to notice this, and so I'm assuming this
> is "correct".  Could anyone please explain:
>
> A) Why leading slashes are not collapsed into a single slash.
> B) Why a leading /../ is not collapsed.
>
Don't see any problem here. What would you like, an absolute path? As a
relative path this works fine for me. If you DO want an absolute path then
try os.path.abspath(). This will go form the root, and should correctly
remove ".." components:

>>> os.path.abspath('.')
'D:\\Python20\\Tools\\idle'
>>> os.path.normpath("../Pythonwin/../Pythonwin/Pythonwin.exe")
'..\\Pythonwin\\Pythonwin.exe'
>>>
os.path.abspath(os.path.normpath("../Pythonwin/../Pythonwin/Pythonwin.exe"))
'D:\\Python20\\Tools\\Pythonwin\\Pythonwin.exe'


> I'm warning you though, I will not buy any platform dependent answers
> since the normpath() funciton is implemented in posixpath.py module
> which is tailored for POSIX systems (i.e. Unices).
>
But, as you can see (if you can excuse the backslashes) it works fine in
Windoze too!

Unix users may wish to confirm that this is good on that platform too.

regards
 Steve





More information about the Python-list mailing list