python对比两个文件

import os ,sys
import json

def Compare_File(file1_path,file2_path):
  file1 = open(os.path.join(file1_path), "r")
  file2 = open(os.path.join(file2_path), "r")

  line1 = file1.readlines()
  line2 = file2.readlines()

  newline1 = []
  newline2 = []

  line1_a=[]
  line2_a=[]
  for item_line1 in line1:
  line1_a.append(item_line1.replace("\n", ""))
  for item_line2 in line2:
  line2_a.append(item_line2.replace("\n", ""))
  for item_line1 in line1_a:
  if item_line1 in line2_a:
    newline1.append(item_line1)
    newline2.append(item_line1)
  else:
    newline1.append(item_line1)
    newline2.append("不存在这一行")
    print(item_line1, "不存在于第二个文件中")
  file1_creat = os.path.basename(file1_path)
  file2_creat = os.path.basename(file2_path)
  with open(os.path.join("./", file1_creat + "01"), "w") as f:
    json.dump(newline1, f, indent=4)
    print("0")
  with open(os.path.join("./", file2_creat + "01"), "w") as f:
    json.dump(newline2, f, indent=4)
    print("1")

def main(file1_path,file2_path):
  Compare_File(file1_path,file2_path)

if __name__ == '__main__':
  file1_path = sys.argv[1]
  file2_path = sys.argv[2]
  main(file1_path,file2_path)
  print("ok")

posted @ 2019-05-13 09:34  AEOK  阅读(521)  评论(0编辑  收藏  举报