Comparing two text files in python

前端 未结 4 1240
执念已碎
执念已碎 2021-02-05 12:20

I need to compare two files and redirect the different lines to third file. I know using diff command i can get the difference . But, is there any way of doing it in python ? An

4条回答
  •  爱一瞬间的悲伤
    2021-02-05 13:03

    import sys
    if len(sys.argv) !=3 :
      print "usage:" + sys.argv[0] + "   bla bla"
      exit
    elif len(sys.argv) == 3:
      file1 = set((x for x in open(sys.argv[1])))
      file2 = set((x for x in open(sys.argv[2])))
      file3 = file2.difference(file1)
      file4 = file1.difference(file2)
      str1="file1-contains but  file2 not \n"
      str2="file2-contains but  file1 not\n"
      FILE = open('file3','w')
      FILE.writelines(str2)
      FILE.writelines(file3)
      FILE.writelines(str1)
      FILE.writelines(file4)
    

提交回复
热议问题