Python/Json - 检查多个文件中的特定对象

Python/Json - Check for a specific object in multiple files

我有大量 json 个文件 (4000),我需要检查每个文件中的每个文件以查找特定对象。我的代码如下:

import os
import json

files = sorted(os.listdir("my files path"))
for f in files:
    if f.endswith(".json"): 
        myFile = open("my path\" + f)
        myJson = json.load(bayesFile)
        if myJson["something"]["something"]["what im looking for"] == "ACTION"
            #do stuff
        myFile.close()

正如您想象的那样,这会占用大量执行时间,我想知道是否有更快的方法...?

这里有一个多线程方法可能对您有所帮助:

from glob import glob
import json
from concurrent.futures import ThreadPoolExecutor
import os

BASEDIR = 'myDirectory' # the directory containing the json files

def process(filename):
    with open(filename) as infile:
        data = json.load(infile)
        if data.get('foo', '') == 'ACTION':
            pass # do stuff

def main():
    with ThreadPoolExecutor() as executor:
        executor.map(process, glob(os.path.join(BASEDIR, '*.json')))

if __name__ == '__main__':
    main()