Python将JSON集合从多个文件夹导出到单独的以制表符分隔的文件中

Python将JSON集合从多个文件夹导出到单独的以制表符分隔的文件中,python,json,Python,Json,我有500个文件夹,每个文件夹包含20-40个不同数量的JSON集合。我能够通过以下给出的答案,在Python中手动和单独提取每个文件夹的内容 然而,考虑到这是必须做500次,这将是相当费劲和显然非常令人厌倦的。最受欢迎的方法是采用更快的自动化方法,将每个JSON文件夹的内容重复提取到以制表符分隔的文件中。谢谢 import os, json current_directory = os.path.dirname(os.path.realpath(__file__)) all_direct

我有500个文件夹,每个文件夹包含20-40个不同数量的JSON集合。我能够通过以下给出的答案,在Python中手动和单独提取每个文件夹的内容

然而,考虑到这是必须做500次,这将是相当费劲和显然非常令人厌倦的。最受欢迎的方法是采用更快的自动化方法,将每个JSON文件夹的内容重复提取到以制表符分隔的文件中。谢谢

import os, json


current_directory = os.path.dirname(os.path.realpath(__file__))

all_directories = [x[0] for x in os.walk(current_directory)]

for directory in all_directories:
    path_to_json = directory
    json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]
    #list all the files in the folder
    print (json_files)

    for js in json_files:
        with open(os.path.join(path_to_json, js)) as json_file:
    #can only print to screen all the json files - need help saving to a tab-delimited file 
           print (json.load(json_file))

这将找到执行此脚本的所有文件夹。然后,它将迭代文件夹并执行此操作。您需要将其复制到存储文件夹的位置。

使用glob模块并找到与所有文件匹配的正确表达式,例如*/*.json.Use os.walk递归遍历文件夹。我尝试编辑代码以将输出保存到以制表符分隔的文件中。见上文
import os, json


current_directory = os.path.dirname(os.path.realpath(__file__))

all_directories = [x[0] for x in os.walk(current_directory)]

for directory in all_directories:
    path_to_json = directory
    json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]
    #list all the files in the folder
    print (json_files)

    for js in json_files:
        with open(os.path.join(path_to_json, js)) as json_file:
    #can only print to screen all the json files - need help saving to a tab-delimited file 
           print (json.load(json_file))