Warm tip: This article is reproduced from serverfault.com, please click

How to read multiple json files stored in a folder into different dictionaries in Python?

发布于 2020-11-28 14:02:42

Suppose there are 20 JSON files in a folder and I want to read & store them into 20 different dictionaries in a python program.

Questioner
DataNoob
Viewed
0
azro 2020-11-28 22:04:41

You may use a list to hold the filenames, then apply the dict reading for each

import json
from pathlib import Path

files = ['a.txt', 'b.txt', 'c.txt']  # ...
dicts = [json.loads(Path(file).read_text()) for file in files]