如何避免Python的os.path.commonprefix的谬误? [英] How to circumvent the fallacy of Python's os.path.commonprefix?
问题描述
我的问题是找到给定文件集的通用 path 前缀.
My problem is to find the common path prefix of a given set of files.
从字面上看,我期望"os.path.commonprefix"能够做到这一点.不幸的是,commonprefix
位于path
中的事实颇具误导性,因为它实际上会搜索字符串前缀.
Literally I was expecting that "os.path.commonprefix" would do just that. Unfortunately, the fact that commonprefix
is located in path
is rather misleading, since it actually will search for string prefixes.
我的问题是,如何才能真正解决路径问题?该问题在此答案(评级很高)中被简短提及,但仅作为旁注和建议的解决方案(附加斜线输入commonprefix的输入),恕我直言有问题,因为它将失败,例如:
The question to me is, how can this actually be solved for paths? The issue was briefly mentioned in this (fairly high rated) answer but only as a side-note and the proposed solution (appending slashes to the input of commonprefix) imho has issues, since it will fail for instance for:
os.path.commonprefix(['/usr/var1/log/', '/usr/var2/log/'])
# returns /usr/var but it should be /usr
为防止其他人陷入同一陷阱,可能有必要在一个单独的问题中进行讨论:是否有一个不依赖于文件系统讨厌检查的简单/可移植解决方案来解决此问题(即,访问commonprefix的结果,并检查它是否是目录,如果不是,则返回结果的os.path.dirname
?
To prevent others from falling into the same trap, it might be worthwhile to discuss this issue in a separate question: Is there a simple / portable solution for this problem that does not rely on nasty checks on the file system (i.e., access the result of commonprefix and check whether it is a directory and if not returns a os.path.dirname
of the result)?
推荐答案
前一段时间,我遇到了这个问题,其中os.path.commonprefix
是字符串前缀,而不是预期的路径前缀.所以我写了以下内容:
Awhile ago I ran into this where os.path.commonprefix
is a string prefix and not a path prefix as would be expected. So I wrote the following:
def commonprefix(l):
# this unlike the os.path.commonprefix version
# always returns path prefixes as it compares
# path component wise
cp = []
ls = [p.split('/') for p in l]
ml = min( len(p) for p in ls )
for i in range(ml):
s = set( p[i] for p in ls )
if len(s) != 1:
break
cp.append(s.pop())
return '/'.join(cp)
通过将'/'
替换为os.path.sep
,可以使其具有更高的可移植性.
it could be made more portable by replacing '/'
with os.path.sep
.
这篇关于如何避免Python的os.path.commonprefix的谬误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!