码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-05-13 12:04:18      阅读:169      评论:0      收藏:0      [点我收藏+]

标签: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 (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()



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

标签:open   input   span   pytho   item   div   odi   文件   max   

原文地址:https://www.cnblogs.com/maxyang2008/p/9031378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!