代码如下:
#Auther:ponny lee
#Time:2021/09/05,0005 19:25
#File:contrust_healthy_code.py
#IDE :PyCharm
import os
import json
diffs = []
def diff_file(path1, path2):
fileName1 = set([_ for _ in os.listdir(path1)])
name1 = []
for i in fileName1:
portion = os.path.splitext(i) # 把文件名拆分为名字和后缀
if portion[1] == ".jpg":
name1.append(portion[0])
if portion[1] == ".png":
name1.append(portion[0])
fileName2 = set([_ for _ in os.listdir(path2)])
name2 = []
for i in fileName2:
portion = os.path.splitext(i) # 把文件名拆分为名字和后缀
if portion[1] == ".jpg":
name2.append(portion[0])
if portion[1] == ".png":
name2.append(portion[0])
Name1 = set(name1)
Name2 = set(name2)
diffs = Name1.difference(Name2)
# Name1对比Name2,Name1中多出来的文件;注意,如果Name2里有Name1中没有的文件,也不会筛选出来
print(diffs)
c_list = list(diffs)
a = open(
r"C:\\Users\\libaolin\\Desktop\\OTHER\\health_code\\healthy_code_of_classmate\\contrust_result\\name_of_no_code.txt",
"w", encoding='UTF-8')
a.write(",".join(c_list))
a.close()
if __name__ == '__main__':
# 参照路径
path1 = 'C:\\Users\\libaolin\\Desktop\\OTHER\\health_code\\healthy_code_of_classmate\\healthy_code_2021_09_05'
# 对比路径
path2 = 'C:\\Users\\libaolin\\Desktop\\OTHER\health_code\\healthy_code_of_classmate\\healthy_code_2021_09_07'
diff_file(path1, path2)
注意事项:文件命名后缀可不一样,但是参考文件和对比文件的命名内容必须一致;否则会无法识别,后期博主会尝试改进!