I'm trying to read multiple json files using python. My files look something like this:
- Inbox
- Jack
- message1.json
- Brad
- message1.json
- Charles
- message1.json
- Emerson
- message1.json
- Luke
- message1.json
- Jack
As you can see, all the json files have the same name, it's just the folder that has a different name. Is there a way to read the inbox and iterate through each folder to get the json file?
So far I have this:
path = '/messages/inbox/'
file= '/message_1.json'
And was thinking something like this:
for i in <something?> :
new_file = path str(i) file
with open('new_file', 'r') as myfile:
data=myfile.read()
obj = json.loads(data)
I know this can't work because python needs to read the path first. How do I get the program to read the path and then iterate through it?
Thank you for taking the time read my question and help in any way you can.
CodePudding user response:
You can use the method os.listdir("relative path to where the folders are")
(take a look at the domumentation) to get all subdirectories in the cwd. And you should not use loads for getting the contents of a file, instead pass the file object to the json.load()
method. Implementing it in your code would be something like this:
import os
for i in os.listdir(path) :
new_file = path i file
my_file = open(new_file,"r")
obj = json.load(my_file)
my_file.close()