使用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()