import os
import shutil
def diff_file(path1, path2):
path = '/Users/XXXXX/Desktop/test3'
fileName1 = set([_ for _ in os.listdir(path1)])
fileName2 = set([_.replace('.htm', '.pdf') for _ in os.listdir(path2) if '.htm' in _])
diffs = fileName1.difference(fileName2) # fileName1对比fileName2,fileName1中多出来的文件;注意,如果fileName2里有fileName1中没有的文件,也不会筛选出来
filePath = [os.path.join(path, i) for i in diffs]
if not os.path.exists(path):
os.mkdir(path)
for file in filePath:
fileName = file.split('/')[-1]
shutil.copy(os.path.join(path1, fileName), '/'.join(file.split('/')[:-1]))
print('复制文件--', fileName)
if __name__ == '__main__':
# 参照路径
path1 = '/Users/XXXXX/Desktop/test1'
# 对比路径
path2 = '/Users/XXXXX/Desktop/test2'
diff_file(path1, path2)