Skip to content

Instantly share code, notes, and snippets.

@mydreambei-ai
Created June 7, 2017 08:03
Show Gist options
  • Save mydreambei-ai/5888807749385d0063af0c7ea739ed0e to your computer and use it in GitHub Desktop.
Save mydreambei-ai/5888807749385d0063af0c7ea739ed0e to your computer and use it in GitHub Desktop.
Compare files line by line like linux command comm
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--action", dest="action", required=True,
choices=["inter", "union", "diff", "sydiff", "rdiff"])
parser.add_argument(
"A", type=argparse.FileType("r"), help="the base file")
parser.add_argument(
"B", type=argparse.FileType("r")
)
args = parser.parse_args()
S = lambda f: set([line.strip() for line in f.readlines()])
O = {"inter": set.__and__, "union": set.__or__, "diff": set.__sub__, "sydiff": set.__xor__,
"rdiff": set.__rsub__}.get(args.action)(S(args.A), S(args.B))
args.A.close()
args.B.close()
for i in O:
print(i, flush=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment