mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Restored commonprefix() semantics.
This commit is contained in:
parent
a48b526745
commit
03c06ee7fc
1 changed files with 13 additions and 23 deletions
|
@ -89,29 +89,6 @@ def dirname(s): return split(s)[0]
|
||||||
def basename(s): return split(s)[1]
|
def basename(s): return split(s)[1]
|
||||||
|
|
||||||
|
|
||||||
# Return the longest prefix of all list elements.
|
|
||||||
# XXX completely untested on Mac!!!
|
|
||||||
|
|
||||||
def commonprefix(m):
|
|
||||||
"Given a list of pathnames, returns the longest common leading component"
|
|
||||||
if not m: return ''
|
|
||||||
n = m[:]
|
|
||||||
for i in range(len(n)):
|
|
||||||
n[i] = n[i].split(os.sep)
|
|
||||||
# if os.sep didn't have any effect, try os.altsep
|
|
||||||
if os.altsep and len(n[i]) == 1:
|
|
||||||
n[i] = n[i].split(os.altsep)
|
|
||||||
|
|
||||||
prefix = n[0]
|
|
||||||
for item in n:
|
|
||||||
for i in range(len(prefix)):
|
|
||||||
if prefix[:i+1] <> item[:i+1]:
|
|
||||||
prefix = prefix[:i]
|
|
||||||
if i == 0: return ''
|
|
||||||
break
|
|
||||||
return os.sep.join(prefix)
|
|
||||||
|
|
||||||
|
|
||||||
def isdir(s):
|
def isdir(s):
|
||||||
"""Return true if the pathname refers to an existing directory."""
|
"""Return true if the pathname refers to an existing directory."""
|
||||||
|
|
||||||
|
@ -166,6 +143,19 @@ def exists(s):
|
||||||
return 0
|
return 0
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
# Return the longest prefix of all list elements.
|
||||||
|
|
||||||
|
def commonprefix(m):
|
||||||
|
"Given a list of pathnames, returns the longest common leading component"
|
||||||
|
if not m: return ''
|
||||||
|
prefix = m[0]
|
||||||
|
for item in m:
|
||||||
|
for i in range(len(prefix)):
|
||||||
|
if prefix[:i+1] <> item[:i+1]:
|
||||||
|
prefix = prefix[:i]
|
||||||
|
if i == 0: return ''
|
||||||
|
break
|
||||||
|
return prefix
|
||||||
|
|
||||||
def expandvars(path):
|
def expandvars(path):
|
||||||
"""Dummy to retain interface-compatibility with other operating systems."""
|
"""Dummy to retain interface-compatibility with other operating systems."""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue