jsonDiff 循环处理大量文件

jsonDiff in a loop for lots of files

目标是找出两个文件夹中大约 30'000 json 个文件的差异。在这两个文件夹中,它们的名称只是数字(1.json、2.json 等)。所以基本上得到两个文件夹的差异。

我正在使用 jsonDiff 模块来获取差异,这对单个文件来说效果很好,但我还没有找到在循环中使用该模块的方法。

f = open("/folder1/1.json")
fe = open("/folder2/1.json")
jyson1 = json.load(f)
jyson2 = json.load(fe)

print(diff(jyson1, jyson2))

编辑: 因为我必须解析 2 个文件夹,所以我想我需要 2 个 for 循环,像这样

for f in glob("/folder1/*.json"):
    with open(f) as fe:
        data_old = json.load(fe)

for e in glob("/folder2/*.json"):
    with open(e) as ee:
        data_new = json.load(fe)

我很难理解如何在这里使用 diff() 方法

这是您可以处理的代码片段。它还只测试两个文件夹中共有的文件(跳过一个文件夹中因任何原因丢失的文件)。与您的代码片段相反,您需要在一个而不是两个 for 循环中工作:

import os

names1 = os.listdir("folder1") # names of all json files in folder1
names2 = os.listdir("folder2") # names of all json files in folder2
commonnames = set(names1) & set(names2) # set of all file names which are in folder1 and folder2

for filename in commonnames:
  json1 = json.load(open(os.path.join("folder1", filename))
  json2 = json.load(open(os.path.join("folder2", filename))

  print(diff(json1, json2))