python对比两个文件不同

pycharm 自带文件对比功能,右键选择compare with
def compare_file(file1,file2):
f1 = open(file1,'r')
f2 = open(file2,'r')
count = 0
diff = []
for line1 in f1:
line2 = f2.readline()
count +=1
if line1 != line2:
diff.append(count)
f1.close()
f2.close()
return diff
file1 = input('输入第一个文件:')
file2 = input('输入第二个文件:')
differ = compare_file(file1,file2)
if len(differ) == 0:
print('两个文件一样')
else:
print('两个文件共有%d处不同' % len(differ))
for each in differ:
print('第%d行不同' % each)

posted on 2020-12-03 10:10  小白在此  阅读(2839)  评论(0编辑  收藏  举报

导航