Peter Otten __peter__ at web.de
Sun Nov 9 10:53:32 CET 2003

Ben Allfree wrote:

> Written to normalize web server path names based on a virtual root. I
> propose that something equivalent to this be added to os.path

The purpose remains unclear to me. Maybe you could expand a little.
For now, I suppose you intend vnormpath() to always return subfolders of

> -------------------------------------
> import os.path
> import copy
> def vnormpath(root,path):
>     """
>     Normalize a path based on a virtual root.
>     """
>     r = copy.deepcopy(root)
>     p = copy.deepcopy(path)

Strings are "immutable", i. e. you can't change them once created. You
therefore need not (I'd rather say must not) copy them.

>     if os.path.isabs(path):
>         return root + path

This can give you funny paths on Windows like "C:/firstC:/second"; on Unix
you can get anywhere the supplier of *path* wants,
e. g. vnormpath("/home/peter", "/../ben") returns "/home/peter/../ben" which
is equivalent to "home/ben".

>     while os.path.commonprefix([root,
>     os.path.normpath(os.path.join(r,p))])
> <> root:
>         r = os.path.join(r,"junk")
>     return os.path.normpath(os.path.join(r,p))

I tried to break that, too, but must admit I didn't succed so far :-)
By the way, most pythonistas favour != over <>.

> if __name__ == "__main__":
>     print vnormpath("C:\\foo\\baz",
> "..\\..\\..\\..\\foo\\baz\\..\\..\\..\\frob\\glop")

The above demo is probably a good start, but try to think of a few more
use/test cases. For example, should
vnormpath("/home/ben", "/home/ben/temp") return "/home/ben/home/ben/temp" or
rather "home/ben/temp"? Also, I would compare vnormpath()'s actual against
the expected result instead of just printing it out. For production quality
code, have a look at the unittest module.

Conclusion: For the moment, be satisfied if you can fix the bugs and your
function serves *your* needs well. If you want to share the results, put it
on a web site.
For your code to make it into the python library, you have to convince
people that *they* need it badly and I dare say that you may face some
strong opposition.


More information about the Python-list mailing list