python 判断两个文件夹有无重复文件

 

import os

"""判断两个文件夹里是否有相同文件名的文件"""


def fileList(path):
    filelist = {}
    n = 0
    for root, folders, files in os.walk(path):
        for file in files:
            n += 1
            filelist[file] = os.path.join(root, file)
    print('\r扫描了 %s 个文件 ------- %s' % (n, path))
    return filelist


def compare(path1, path2):
    dict1 = fileList(path1)
    dict2 = fileList(path2)
    print('----------------------------------相同文件---------------------------------')
    for key in dict1:
        if key in dict2:
            print(dict1[key], dict2[key], sep='  <---->  ')


if __name__ == '__main__':
    path1 = r'路径一'
    path2 = r'路径二'

    compare(path1, path2)

    print("完成.")

 

你可能感兴趣的:(python基础)