1

我想在两个不同的文件夹之间找到大小写不匹配的文件名。

例如,folder1 包含:

file1.txt
file2.txt
file3.txt

文件夹 2 包含:

FILE1.txt
file2.txt
file3.TXT

结果应该是:

FILE1.txt or file1.txt
file3.TXT or file3.txt
4

2 回答 2

1

假设您在列表中有文件名,称为ab。然后您可以遍历两者并比较区分大小写的文件名,如果它们匹配,则中断循环,如果它们不匹配但如果将所有字符更改为小写它们将匹配,则打印文件名,即

for file1 in a:
    for file2 in b:
        if file1==file2: break
        if file1.lower()==file2.lower():
            print '%s or %s' % (file1,file2)
            break
于 2013-06-17T09:06:58.460 回答
0
fileNames1 = os.listdir('path1')
fileNames2 = os.listdir('path2')
fileNames1LC = { original.lower(): original for original in fileNames1 }
fileNames2LC = { original.lower(): original for original in fileNames2 }
for lc, original1 in fileNames1LC.iteritems():
    try:
        original2 = fileNames2LC[lc]
    except KeyError:
        # file in path1 found which has no partner in path2
        pass  # do whatever you want to do then
    else:  # found a partner in path2
        if original1 != original2:  # found different capitalisation
            print original2, "or", original1

通过在pass语句中输入 sth,您可以处理在 中出现path1但在 中没有伙伴的文件path2。如果您需要处理在 中出现path2且在 中没有伙伴的文件path1,您也必须步行fileNames2LC

于 2013-06-17T09:02:01.453 回答