Note that there are some explanatory texts on larger screens.

plurals
  1. POSearch for sub-directory python
    primarykey
    data
    text
    <p>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</p> <p>While researching how to do this, I came across <a href="https://stackoverflow.com/questions/973473/getting-a-list-of-all-subdirectories-in-the-current-directory">this question</a> and started using <code>os.walk(dir).next()[1]</code> to list directories. This had limited success. As the script searched through directories, it would essentially give up and break after, giving the <code>StopIteration</code> error. Sample output is below searching for a sub-directory within <code>TEST1</code>.</p> <pre><code>C:\Python27&gt;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 &lt;module&gt; 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 </code></pre> <p><code>curDir</code> 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.</p> <p>I can provide my code if required, but didn't want to initially post it to avoid an even bigger wall of text. </p> <p>My question is: why does the script give up after searching a few folders? Thanks in advance for your help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload