Fredrik Lundh wrote:
mal wrote:> > Ooh, very likely:
os.path.normpath('//5/foo/bar') '/5/foo/bar'
Isn't // at the root a Unix convention of some sort for some network filesystems? Probably normpath() should just leave it alone.
Samba uses //<hostname>/<mountname>/<path>. os.path.normpath() should probably leave the leading '//' untouched (having too many of those in the path doesn't do any harm, AFAIK).
from 1.5.2's posixpath:
def normpath(path): """Normalize path, eliminating double slashes, etc.""" import string # Treat initial slashes specially slashes = '' while path[:1] == '/': slashes = slashes + '/' path = path[1:] ... return slashes + string.joinfields(comps, '/')
from 2.0's posixpath:
def normpath(path): """Normalize path, eliminating double slashes, etc.""" if path == '': return '.' import string initial_slash = (path[0] == '/') ... if initial_slash: path = '/' + path return path or '.'
interesting...
Here's the log message: revision 1.34 date: 2000/07/19 17:09:51; author: montanaro; state: Exp; lines: +18 -23 added rewritten normpath from Moshe Zadka that does the right thing with paths containing .. and the diff: diff -r1.34 -r1.33 349,350d348 < if path == '': < return '.' 352,367c350,372 < initial_slash = (path[0] == '/') < comps = string.split(path, '/') < new_comps = [] < for comp in comps: < if comp in ('', '.'): < continue < if (comp != '..' or (not initial_slash and not new_comps) or < (new_comps and new_comps[-1] == '..')): < new_comps.append(comp) < elif new_comps: < new_comps.pop() < comps = new_comps < path = string.join(comps, '/') < if initial_slash: < path = '/' + path < return path or '.' ---
# Treat initial slashes specially slashes = '' while path[:1] == '/': slashes = slashes + '/' path = path[1:] comps = string.splitfields(path, '/') i = 0 while i < len(comps): if comps[i] == '.': del comps[i] while i < len(comps) and comps[i] == '': del comps[i] elif comps[i] == '..' and i > 0 and comps[i-1] not in ('', '..'): del comps[i-1:i+1] i = i-1 elif comps[i] == '' and i > 0 and comps[i-1] <> '': del comps[i] else: i = i+1 # If the path is now empty, substitute '.' if not comps and not slashes: comps.append('.') return slashes + string.joinfields(comps, '/')
Revision 1.33 clearly leaves initial slashes untouched. I guess we should restore this... -- Marc-Andre Lemburg ______________________________________________________________________ Company: http://www.egenix.com/ Consulting: http://www.lemburg.com/ Python Pages: http://www.lemburg.com/python/