dumbchild
dumbchild

Reputation: 285

read mutiple json files from multiple directories

i have multiple directories which all contain JSON files. I know how I could read all in ONE directory, but not how to read them in all directories without specifying the dirctory names.

I played around and came up with something like this:

import json
import os

path_to_json = 'path/to/dir/with/dirs' 
json_files = [pos_json for pos_json in os.listdir(path_to_json)]


for json_file in json_files:
    filename = str(json_file + "/") # here something like "*.json"
    with open(filename, 'r') as myfile:
        data=myfile.read()

any help is greatly appreciated

Upvotes: 1

Views: 558

Answers (2)

beastlybananas
beastlybananas

Reputation: 1

You can use the os.walk and give the top level directory as the directory_name.

import os

root = "<path-to-dir>"

for path, subdirs, files in os.walk(root):
    for filename in files:
        if filename.endswith('.json'):
            with open(filename, 'r') as myfile:
                data = myfile.read()

Upvotes: 0

Rakesh
Rakesh

Reputation: 82755

Use os.walk with str.endswith

Ex:

path_to_json = 'path/to/dir/with/dirs' 
json_files = []

for root, dirs, files in os.walk(path_to_json):
    for f in files:
        if f.endswith('.json'):      #Check for .json exten
            json_files.append(os.path.join(root, f))    #append full path to file
            
for json_file in json_files:
    with open(json_file, 'r') as myfile:
        data=myfile.read()  

Upvotes: 2

Related Questions