用python比较两个文件中内容的不同之处, 并输出行号和内容.

代码部分:
'''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')
执行结果
a1.txt和a2.txt第4行不同, 内容为:  --> 4444444444444444444
a1.txt和a2.txt第5行不同, 内容为: sfsdfasdf --> 5555555555555555555
a1.txt和a2.txt第7行不同, 内容为: fasdfasdf --> sfsdfasdf
a1.txt和a2.txt第8行不同, 内容为: 1111111111111111111 --> 
a1.txt和a2.txt第9行不同, 内容为: 2222222222222222222 --> fasdfasdf
a1.txt和a2.txt第10行不同, 内容为: 3333333333333333333 --> 
a1.txt和a2.txt第11行不同, 内容为: 4444444444444444444 --> 
a1.txt和a2.txt第12行不同, 内容为: 5555555555555555555 --> 
a1.txt和a2.txt第13行不同, 内容为:  --> 
a1.txt和a2.txt第14行不同, 内容为: sfsdfasdf --> 
a1.txt和a2.txt第15行不同, 内容为:  --> 
a1.txt和a2.txt第16行不同, 内容为: fasdfasdf --> 
a1.txt和a2.txt第17行不同, 内容为: 1111111111111111111 --> 
a1.txt和a2.txt第18行不同, 内容为: 2222222222222222222 --> 
a1.txt和a2.txt第19行不同, 内容为: 3333333333333333333 --> 
a1.txt和a2.txt第20行不同, 内容为: 4444444444444444444 --> 
a1.txt和a2.txt第21行不同, 内容为: 5555555555555555555 --> 
a1.txt和a2.txt第22行不同, 内容为:  --> 
a1.txt和a2.txt第23行不同, 内容为: sfsdfasdf --> 
a1.txt和a2.txt第24行不同, 内容为:  --> 
a1.txt和a2.txt第25行不同, 内容为: fasdfasdf --> 
a1.txt和a2.txt第26行不同, 内容为: 1111111111111111111 --> 
a1.txt和a2.txt第27行不同, 内容为: 2222222222222222222 --> 
a1.txt和a2.txt第28行不同, 内容为: 3333333333333333333 --> 
a1.txt和a2.txt第29行不同, 内容为: 4444444444444444444 --> 
a1.txt和a2.txt第30行不同, 内容为: 5555555555555555555 --> 
a1.txt和a2.txt第31行不同, 内容为:  --> 
a1.txt和a2.txt第32行不同, 内容为: sfsdfasdf --> 
a1.txt和a2.txt第33行不同, 内容为:  --> 
a1.txt和a2.txt第34行不同, 内容为: fasdfasdf --> 

转载于:https://blog.51cto.com/13501622/2065159

  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值