(path)
return prefix + backslash.join(comps)
Normalize path, eliminating double slashes, etc.
Normalize path, eliminating double slashes, etc.
Normalize path eliminating double slashes etc .
def normpath(path):
    """Normalize path, eliminating double slashes, etc."""
    # Preserve unicode (if path is unicode)
    backslash, dot = (u'\\', u'.') if isinstance(path, unicode) else ('\\', '.')
    if path.startswith(('\\\\.\\', '\\\\?\\')):
        # in the case of paths with these prefixes:
        # \\.\ -> device names
        # \\?\ -> literal paths
        # do not do any normalization, but return the path unchanged
        return path
    path = path.replace("/", "\\")
    prefix, path = splitdrive(path)
    # We need to be careful here. If the prefix is empty, and the path starts
    # with a backslash, it could either be an absolute path on the current
    # drive (\dir1\dir2\file) or a UNC filename (\\server\mount\dir1\file). It
    # is therefore imperative NOT to collapse multiple backslashes blindly in
    # that case.
    # The code below preserves multiple backslashes when there is no drive
    # letter. This means that the invalid filename \\\a\b is preserved
    # unchanged, where a\\\b is normalised to a\b. It's not clear that there
    # is any better behaviour for such edge cases.
    if prefix == '':
        # No drive letter - preserve initial backslashes
        while path[:1] == "\\":
            prefix = prefix + backslash
            path = path[1:]
    else:
        # We have a drive letter - collapse initial backslashes
        if path.startswith("\\"):
            prefix = prefix + backslash
            path = path.lstrip("\\")
    comps = path.split("\\")
    i = 0
    while i < len(comps):
        if comps[i] in ('.', ''):
            del comps[i]
        elif comps[i] == '..':
            if i > 0 and comps[i-1] != '..':
                del comps[i-1:i+1]
                i -= 1
            elif i == 0 and prefix.endswith("\\"):
                del comps[i]
            else:
                i += 1
        else:
            i += 1
    # If the path is now empty, substitute '.'
    if not prefix and not comps:
        comps.append(dot)
    return prefix + backslash.join(comps)
def normpath ( path ) : # Preserve unicode (if path is unicode) backslash , dot = ( u'\\' , u'.' ) if isinstance ( path , unicode ) else ( '\\' , '.' ) if path . startswith ( ( '\\\\.\\' , '\\\\?\\' ) ) : # in the case of paths with these prefixes: # \\.\ -> device names # \\?\ -> literal paths # do not do any normalization, but return the path unchanged return path path = path . replace ( "/" , "\\" ) prefix , path = splitdrive ( path ) # We need to be careful here. If the prefix is empty, and the path starts # with a backslash, it could either be an absolute path on the current # drive (\dir1\dir2\file) or a UNC filename (\\server\mount\dir1\file). It # is therefore imperative NOT to collapse multiple backslashes blindly in # that case. # The code below preserves multiple backslashes when there is no drive # letter. This means that the invalid filename \\\a\b is preserved # unchanged, where a\\\b is normalised to a\b. It's not clear that there # is any better behaviour for such edge cases. if prefix == '' : # No drive letter - preserve initial backslashes while path [ : 1 ] == "\\" : prefix = prefix + backslash path = path [ 1 : ] else : # We have a drive letter - collapse initial backslashes if path . startswith ( "\\" ) : prefix = prefix + backslash path = path . lstrip ( "\\" ) comps = path . split ( "\\" ) i = 0 while i < len ( comps ) : if comps [ i ] in ( '.' , '' ) : del comps [ i ] elif comps [ i ] == '..' : if i > 0 and comps [ i - 1 ] != '..' : del comps [ i - 1 : i + 1 ] i -= 1 elif i == 0 and prefix . endswith ( "\\" ) : del comps [ i ] else : i += 1 else : i += 1 # If the path is now empty, substitute '.' if not prefix and not comps : comps . append ( dot ) return prefix + backslash . join ( comps )