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")

转载于:https://www.cnblogs.com/AEBOKE/p/10854934.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值