os.path.normpath()

Gawain Bolton gbolton at wanadoo.fr
Sat Jan 27 09:20:37 EST 2001


Steve Holden wrote:

> "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

No I already have an "absolute path".  An absolute path is merely one that
begins with a leading slash.  And if you take a look at the documentation for
normpath, it says:

normpath (path)
     Normalize a pathname. This collapses redundant separators and up-level
references, e.g. A//B, A/./B and A/foo/../B all become A/B. It does not
normalize
     the case (use normcase() for that). On Windows, it converts forward
slashes to backward slashes.

So normpath() should remove the unnecessary /../ and multiple leading slashes.

I downloaded and compiled Python 2.0 for Sun and found that normpath() in
Python 2.0 does not suffer from these problems!  Therefore, this must be in a
bug Python 1.5 and 1.5.2 and probably earlier versions as well.


Gawain





More information about the Python-list mailing list