小事牛刀之——python做文件對比


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

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : file_diff.py
# @Author: Maxwell Yang (maxyang2008@163.com)
# @Date  : 2018/4/10
# @Desc  : 從文件2中去除掉在文件1中有的行,生成文件3

filename1 = 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()




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM