主要是判断两个工程的差异,类似github中看不同branch的差异的功能,但是这个就是输出有差异的文件名称,也能改成输出有差异的代码,但是如果多的话,看着乱,就光输出脚本名称了。
import os
def compare(l1:str,l2:str)->str:
ll = ''
if l1 != l2:
ll = l2
else:
ll = ''
return ll
def check_line(string:str)->bool:
flag_num = False
flag_alpha = False
for i in string:
if i.isdigit():
flag_num = True
if i.isalpha():
flag_alpha = True
return flag_alpha and flag_num
if __name__ == '__main__':
new_dir = str(path_a)
ori_dir = str(path_b)
for dirpath, dirnames, filenames in os.walk(ori_dir):
for filename in filenames:
if filename.endswith('.py'):
ori_path = str(os.path.join(dirpath, filename))
new_path = str(os.path.join(dirpath, filename)).replace(ori_dir,new_dir)
# 查看两个文件是否对应
if os.path.exists(ori_path) and os.path.exists(new_path):
f1 = open(ori_path, 'r', encoding='UTF-8')
f2 = open(new_path, 'r', encoding='UTF-8')
# 检查差异,并输出脚本名称
with open("diff.txt","a", encoding='UTF-8') as f:
try:
while True:
t1 = f1.readline()
t2 = f2.readline()
if t1 or t2:
res = compare(t1,t2)
if res != '':
f.write(filename+'\n')
break
else:
break
finally:
f1.close()
f2.close()