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

使用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中有的行,生成文件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()



猜你喜欢

转载自www.cnblogs.com/maxyang2008/p/9031378.html