Python在给定文件夹中找到类似的文件

时间:2015-07-11 11:50:29

标签: python

我试图编写一个函数,它会在指定的文件夹中找到一个名字(song.mp3,song1.mp3,(1)song.mp3)的类似文件。我现在有什么:

def print_duplicates(source):
    files_list = []
    new_list = []

    for dirpath, dirnames, filenames in os.walk(source):
        for fname in filenames:
            if ('\w*' + fname + '\w*') in files_list:
                new_list.append(os.path.join(dirpath, fname))
            else:
                files_list.append(fname)

    for a in new_list:
        print(a)

如果文件名在files_list之前不是,那么它将被添加,如果它将被添加到new_list及其路径中。通过这种方式,我列出了重复的'文件。但是它没有工作,new_list仍然是空的。 你能纠正我的错误吗?我的代码中哪一部分错了?

1 个答案:

答案 0 :(得分:2)

如果要在代码中使用正则表达式,则需要使用re模块。

所以改变这一行,

if ('\w*' + fname + '\w*') in files_list:

要,

if re.search(r'\w*' + fname + r'\w*', files_list):

完全相同
if fname in file_list:

因为\w*表示零个或多个单词字符。而且我认为你想使用单词边界。

if re.search(r'\b' + fname + r'\b', files_list):