用python比較兩個檔案中內容的不同之處, 並輸出行號和內容.

來源:互聯網
上載者:User

標籤:dfa   列印   asd   path   fas   檔案   port   __init__   pat   

代碼部分:
‘‘‘cmpfile.py - 比對兩個檔案, 如果有不同之處, 列印內容和行號‘‘‘import osclass 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():            fp1 = open(self.file1)            fp2 = open(self.file2)            flist1 = [i for i in fp1]            flist2 = [x for x in fp2]        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 cmpresesif __name__ == ‘__main__‘:    cmpfile = cmpFile(‘a1.txt‘, ‘a2.txt‘)    difflines = cmpfile.compare()    for i in difflines:        print(i, end=‘\n‘)
執行結果
a1.txt和a2.txt第4行不同, 內容為:  --> 4444444444444444444a1.txt和a2.txt第5行不同, 內容為: sfsdfasdf --> 5555555555555555555a1.txt和a2.txt第7行不同, 內容為: fasdfasdf --> sfsdfasdfa1.txt和a2.txt第8行不同, 內容為: 1111111111111111111 --> a1.txt和a2.txt第9行不同, 內容為: 2222222222222222222 --> fasdfasdfa1.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 --> 

用python比較兩個檔案中內容的不同之處, 並輸出行號和內容.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.