Search for sub-directory python

python

Solution

`StopIteration` is raised whenever an iterator has no more values to generate.

Why are you using `os.walk(dir).next()[1]`? Wouldn't it be easier to just do everything in a for loop? Like:

for root, dirs, files in os.walk(mydir):
    #dirs here should be equivalent to dirList

Here is the documentation for `os.walk`.

Problem

I am attempting to automate the specification of a sub-directory which one of my scripts requires. The idea is to have the script search the C: drive for a folder of a specific name. In my mind, this begs for a recursive search function. The plan is to check all sub-directories, if none are the desired directory, begin searching the sub-directories of the current sub-directories While researching how to do this, I came across this question and started using `os.walk(dir).next()[1]` to list directories. This had limited success. As the script searched through directories, it would essentially give up and break after, giving the `StopIteration` error. Sample output is below searching for a sub-directory within `TEST1`. ``` C:\Python27>test.py curDir: C:\Python27 ['DLLs', 'Doc', 'include', 'Lib', 'libs', 'pyinstaller-2.0', 'Scripts', 'tcl', 'TEST1', 'Tools'] curDir: DLLs [] curDir: Doc [] curDir: include [] curDir: Lib ['bsddb', 'compiler', 'ctypes', 'curses', 'distutils', 'email', 'encodings', 'hotshot', 'idlelib', 'importlib', 'json', 'lib-tk', 'lib2to3', 'logging', 'msilib', 'multiprocessing', 'pydoc_data', 'site-packages', 'sqlite3', 'test', 'unittest', 'wsgiref', 'xml'] curDir: bsddb Traceback (most recent call last): File "C:\Python27\test.py", line 24, in <module> if __name__ == "__main__": main() File "C:\Python27\test.py", line 21, in main path = searcher(os.getcwd()) File "C:\Python27\test.py", line 17, in searcher path = searcher(entry) File "C:\Python27\test.py", line 17, in searcher path = searcher(entry) File "C:\Python27\test.py", line 6, in searcher dirList = os.walk(dir).next()[1] StopIteration ``` `curDir` is the the current directory that is being searched and the next line of output is the list of subdirectories. Once the program finds a directory with no sub-directories, it kicks back up one level and goes to the next directory. I can provide my code if required, but didn't want to initially post it to avoid an even bigger wall of text. My question is: why does the script give up after searching a few folders? Thanks in advance for your help!

Original source

Related problems