小事牛刀之——python做檔案對比

來源:互聯網
上載者:User

標籤:open   input   span   pytho   item   div   odi   檔案   max   

使用python對比filename1和filenam2的差異,並將差異寫入到filename3中。

#!/usr/bin/env python# -*- coding: utf-8 -*-# @File  : file_diff.py# @Author: Maxwell Yang ([email protected])# @Date  : 2018/4/10# @Desc  : 從檔案2中去除掉在檔案1中有的行,組建檔案3filename1 = input(‘請輸入需要剔除內容的檔案路徑:‘)filename2 = input(‘請輸入作為對比的檔案路徑:‘)filename3 = input(‘請輸入存放2檔案差異的檔案路徑:‘)f1 = open(filename1,‘r‘)f2 = open(filename2,‘r‘)f3 = open(filename3,‘w‘)list1 = list(f1)list2 = list(f2)list3 = []for each in list2:    #print(each)    if each not in list1:        list3.append(each)for item in list3:    f3.write(item)f1.close()f2.close()f3.close()



小事牛刀之——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.