B
Ben Allfree
Written to normalize web server path names based on a virtual root. I
propose that something equivalent to this be added to os.path
-------------------------------------
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)
if os.path.isabs(path):
return root + path
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))
if __name__ == "__main__":
print vnormpath("C:\\foo\\baz",
"..\\..\\..\\..\\foo\\baz\\..\\..\\..\\frob\\glop")
propose that something equivalent to this be added to os.path
-------------------------------------
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)
if os.path.isabs(path):
return root + path
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))
if __name__ == "__main__":
print vnormpath("C:\\foo\\baz",
"..\\..\\..\\..\\foo\\baz\\..\\..\\..\\frob\\glop")