用户工具


#!/usr/bin/env python
##################################################
#                                                #
#  desc:    find out diff record between to file #
#  author:  fangqiang                            #
#  date:    2014-01-02                           #
#                                                #
##################################################

from argparse import ArgumentParser

class FileDiff(object):

    def __init__(self, file1, file2):

        self.file1 = file1
        self.file2 = file2

    def file_to_set(self, file):
        lines = open(file).readlines()
        lines = [ line.strip() for line in lines if len(line.strip()) != 0 ]
        return set(lines)

    def sub(self):
        return list(self.file_to_set(self.file1) - self.file_to_set(self.file2))

    def union(self):
        return list(self.file_to_set(self.file1) | self.file_to_set(self.file2))

    def intersection(self):
        return list(self.file_to_set(self.file1) & self.file_to_set(self.file2))


if __name__ == "__main__":
    p = ArgumentParser(usage='find different line between two file', description='fill diff')
    p.add_argument('-file1',  default="", type=str, required=True,help='first file') 
    p.add_argument('-file2',  default="", type=str, required=True,help='sencond file') 
    p.add_argument('-type',  default="", choices=["sub","intersection","union"], type=str, required=True, help='action') 
    args = p.parse_args()

    fd = FileDiff(args.file1, args.file2)
    eval_str = "fd.%s()" % (args.type)
    res = eval(eval_str)
    
    for line in res:
        print line