I am trying to create a loop in python which will allow me to open a folder, iterate through the subfolders within it, read the json files and output them as a csv. Then repeat the loop for each subfolder.
My directory looks like this:
Main folder = "Exports"
Subfolder = "Folder1" , "Folder2" etc..
Files within subfolder = "file1.json" , "file2.json" etc...
Currently I am running the following code within a subfolder (for example "Folder1") to create an output file:
import pandas as pd
import os
path = os.getcwd()
frame = pd.DataFrame()
for filename in os.listdir(os.getcwd()):
root, ext = os.path.splitext(filename)
if ext == '.json':
tmp_frame = pd.read_json(filename)
frame = frame.append(tmp_frame, ignore_index=True)
frame.to_csv(os.path.join(path + ".csv"))
My question is how do I run that loop but within the main folder where it will open each subfolder, then run that loop and output the file as csv for each subfolder.
Thanks