'''cmpfile.py - 比对两个文件, 如果有不同之处, 打印内容和行号''' import os class cmpFile: def __init__(self, file1, file2): self.file1 = file1 self.file2 = file2 def fileExists(self): if os.path.exists(self.file1) and os.path.exists(self.file2): return True else: return False # 对比文件不同之处, 并返回结果 def compare(self): if cmpFile(self.file1, self.file2).fileExists() == False: return [] fp1 = open(self.file1) fp2 = open(self.file2) flist1 = [i for i in fp1] flist2 = [x for x in fp2] fp1.close() fp2.close() flines1 = len(flist1) flines2 = len(flist2) if flines1 < flines2: flist1[flines1:flines2+1] = ' ' * (flines2 - flines1) if flines2 < flines1: flist2[flines2:flines1+1] = ' ' * (flines1 - flines2) counter = 1 cmpreses = [] for x in zip(flist1, flist2): if x[0] == x[1]: counter +=1 continue if x[0] != x[1]: cmpres = '%s和%s第%s行不同, 内容为: %s --> %s' % (self.file1, self.file2, counter, x[0].strip(), x[1].strip()) cmpreses.append(cmpres) counter +=1 return cmpreses if __name__ == '__main__': cmpfile = cmpFile('a1.txt', 'a2.txt') difflines = cmpfile.compare() for i in difflines: print(i, end='n')
以上文件针对txt文件。
但是我对比的是python文件
我对比的是py文件。为了使用该程序,将*.py文件改为*.txt文件
结果报错:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 338: illegal multibyte sequence等相关错误。
错误地点:
fp1 = open(self.file1)
fp2 = open(self.file2)
我猜应该是打开文件的权限和文件缩进有问题。
将上面的代码改为:
fp1 = open(self.file1,"r",encoding='UTF-8')fp2 = open(self.file2,"r",encoding='UTF-8')
运行成功。