Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(169737)

Unified Diff: Lib/genericpath.py

Issue 4755: Common path prefix
Patch Set: Created 10 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
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.
« Doc/library/os.path.rst ('K') | « Doc/library/os.path.rst ('k') | Lib/macpath.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+