Index: Lib/genericpath.py =================================================================== --- Lib/genericpath.py (revision 67929) +++ Lib/genericpath.py (working copy) @@ -66,7 +66,7 @@ # Return the longest prefix of all list elements. def commonprefix(m): - "Given a list of pathnames, returns the longest common leading component" + "Given a list of pathnames, returns the longest common leading string" if not m: return '' s1 = min(m) s2 = max(m) @@ -75,6 +75,26 @@ return s1[:i] return s1 +# Return the longest prefix of all list elements. +def commonpathprefix(m, separator=None): + """Given a list of pathnames, returns the longest common leading component. + + Unlike commonprefix(), commonpathprefix() considers path elements as + delimited by the defined separator (default: os.sep). + """ + if not m: + return '' + if separator is None: + import os + separator = os.sep + common = [] + s1 = min(m).split(separator) + s2 = max(m).split(separator) + while s1 and s2 and s1[0] == s2[0]: + common.append(s1[0]) + del s1[0], s2[0] + return separator.join(common) + # Split a path in root and extension. # The extension is everything starting at the last dot in the last # pathname component; the root is everything before that.