用python 命令: python test.py a.txt b.txt -o c.txt 写出以下需求:文件a.txt 有一列数据,文件b.txt有一列数据,列出两列数据不一样的数据并输出为新文件从c.txt
#!/usr/bin/env python3
import sys, argparse
ap = argparse.ArgumentParser()
ap.add_argument("file1", help="first file")
ap.add_argument("file2", help="second file")
ap.add_argument("--output", "-o", help="Output file name")
args = ap.parse_args()
f1 = open(args.file1, "r")
f2 = open(args.file2, "r")
file1_set = set([x for x in f1.read().splitlines()])
file2_set = set([x for x in f2.read().splitlines()])
differences = file1_set.symmetric_difference(file2_set)
if args.output:
ofile = open(args.output, "w")
for line in differences:
ofile.write("{}\n".format(line))
ofile.close()
else:
for line in differences:
print(line)
网友评论