好的,所以我需要做的是对 python 3.4 中的文件路径列表进行排序。它们需要按字母顺序排列,但子文件夹及其内容排在第一位
示例输出:
b/e/f.txt
b/d.txt
g/u.txt
i/a/q.txt
a.txt
c.txt
d.txt
Run Code Online (Sandbox Code Playgroud)
在过去的几个小时里,我一直试图通过 Google 找出如何做到这一点,但没有运气
恐怕我目前无法访问 v2 解释器,所以我无法验证其正确性,但在 v2 中它看起来像这样:
def FileComp(File1, File2):
if File1.count('/') == File2.count('/'):
return File1 < File2;
else
Same = 0;
FilePath1 = os.path.dirname(File1);
FilePath2 = os.path.dirname(File2);
FilePath1Len = len(FilePath1);
FilePath2Len = len(FilePath2);
while Same < FilePath1Len and Same < FilePath2Len and FilePath1[Same:Same] == FilePath2[Same:Same]:
Same += 1;
FilePath1 = FilePath1[Same:];
FilePath2 = FilePath2[Same:];
if len(FilePath1) == 0 or len(FilePath2) == 0:
return len(FilePath1) > len(FilePath2); …Run Code Online (Sandbox Code Playgroud)