0

I want to iterate subdirectories within a directory and get all of the config.json files at the root of each subdirectory. I am having trouble with stopping the walk function at the first subdirectory. An example of my folder structure is //abc001/e$/Intepub/wwwroot/Apps/Dev/Region-1 is the rootdir while I want to get config.jsons out of folders like this //abc001/e$/Intepub/wwwroot/Apps/Dev/Region-1/website1/config.json //abc001/e$/Intepub/wwwroot/Apps/Dev/Region-1/website2/config.json //abc001/e$/Intepub/wwwroot/Apps/Dev/Region-1/website3/config.json

Here is the code that I have been working with; it returns all folders within the rootdir and gives me an IOError. How do I stop the iteration and resolve the IOError?

import os
rootdir = r'//abc001/e$/Intepub/wwwroot/Apps/Dev/Region-1'
for subdir, dirs, files in os.walk(rootdir):
    for dir in dirs:
        print dir
martineau
  • 119,623
  • 25
  • 170
  • 301
gwest
  • 37
  • 7
  • Does this answer your question? [Iterating through directories with Python](https://stackoverflow.com/questions/19587118/iterating-through-directories-with-python) – AMC Jan 17 '20 at 18:58

1 Answers1

0

I think you want something like this.

import os
rootdir = r'//abc001/e$/Intepub/wwwroot/Apps/Dev/Region-1'
for subdir, dirs, files in os.walk(rootdir):
    if 'config.json' in files:
        dirs[:] = []
        print(os.path.join(rootdir, subdir, 'config.json')
Matt Shin
  • 424
  • 2
  • 7