永远不要重写提供给您的内容
os.path.commonprefix:
返回最长的路径前缀(一个字符一个字符),它是列表中所有路径的前缀。如果列表为空,则返回空字符串(
'')。请注意,这可能返回无效路径,因为它一次可以处理一个字符。
为了与其他答案进行比较,下面是代码:
# 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 '' s1 = min(m) s2 = max(m) for i, c in enumerate(s1): if c != s2[i]: return s1[:i] return s1



