Python 判断两个文件夹有无同名文件

import os


"""判断两个文件夹里是否有相同文件名的文件"""
def fileList(path):
    filelist = {}
    n = 1
    for root,folders,files in os.walk(path):
        for file in files:
            print('\rHas scanned %s files ------- %s' % (n,path) ,end='')
            n += 1
            filelist[file] = os.path.join(root,file)
    print('\n')
    return filelist


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

            
if __name__ == '__main__':
    path1 = input('Path1:')
    path2 = input('Path2:')

    compare(path1,path2)
            
    print("Done.")
posted @ 2020-02-26 09:29  wztshine  阅读(1780)  评论(0编辑  收藏  举报