0

I'm new to Python. I have to open a folder which contains several folders. Then inside each folder of the several folders there is one folder contains the files that i want to read. i need to open the folder then loop through the several folders inside then open the folder in each of them then open the files. i don't really know how to start. can anyone please help me where to start. i'm trying the following but got really stuck path_to_json = r'main folder'

for file_name in os.listdir(path_to_json):
    if file_name.endswith(".json"):
        print(file_name)
    else:
        current_path = "".join((path_to_json, "/", file_name))
        if os.path.isdir(current_path):
            scan_folder(current_path)
RIG Code
  • 25
  • 1
  • 4
  • Possible duplicate of [Python: How to check folders within folders?](https://stackoverflow.com/questions/51896218/python-how-to-check-folders-within-folders) – chris Aug 04 '19 at 23:47

1 Answers1

1

Walk is a good way to do this:

for root_dir, _, file_names in os.walk(starting_directory):
    for file_name in file_names:
        if file_name.endswith('.json'):
            print(f'{root_dir}/{file_name}')
doticatto
  • 71
  • 5