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

Unified Diff: Lib/ntpath.py

Issue 4755: Common path prefix
Patch Set: Created 10 years, 10 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/ntpath.py
===================================================================
--- Lib/ntpath.py (revision 67929)
+++ Lib/ntpath.py (working copy)
@@ -64,6 +64,26 @@
else:
return ':'
+# 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:
+ separator = r"\\|/"
+ import re
+ common = []
+ s1 = re.split(separator, min(m))
+ s2 = re.split(separator, max(m))
+ while s1 and s2 and s1[0] == s2[0]:
+ common.append(s1[0])
+ del s1[0], s2[0]
+ return "\\".join(common)
+
# Normalize the case of a pathname and map slashes to backslashes.
# Other normalizations (such as optimizing '../' away) are not done
# (this is done by normpath).
« Doc/library/os.path.rst ('K') | « Lib/macpath.py ('k') | Lib/os2emxpath.py » ('j') | no next file with comments »

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