traversing folders, several subfolders for the files in python
<p>I've a folder structure similar to what's outlined below.</p>

<pre><code>Path
|
|
+----SubDir1
|       |
|       +---SubDir1A
|       |       |
|       |       |----- FileA.0001.ext
|       |       |----- ...
|       |       |----- ...
|       |       |----- FileA.1001.ext
|       |       |----- FileB.0001.ext
|       |       |----- ...
|       |       |----- ...
|       |       |----- FileB.1001.ext
|       +---SubDir1B
        |
|       |       |----- FileA.0001.ext
|       |       |----- ...
|       |       |----- ...
|       |       |----- FileA.1001.ext
|       |       |----- FileB.0001.ext
|       |       |----- ...
|       |       |----- ...
|       |       |----- FileB.1001.ext
+----SubDir2
|       |
|       |----- FileA.0001.ext
|       |----- ...
|       |----- ...
|       |----- FileA.1001.ext
|       |----- FileB.0001.ext
|       |----- ...
|       |----- ...
|       |----- FileB.1001.ext
</code></pre>

<p>I want to be able to list the first FileA and first FileB for each SubDir1 and SubDir2</p>

<p>I've looked online and seen os.walk in a for loop, similar to:</p>

<pre><code>import os

rootDir = '.'
for dirName, subdirList, fileList in os.walk(rootDir):
    print('Found directory: %s' % dirName)
    for fname in fileList:
        print('\t%s' % fname)
    # Remove the first entry in the list of sub-directories
    # if there are any sub-directories present
    if len(subdirList) &gt; 0:
        del subdirList[0
</code></pre>

<p>But that seems to only work if there's a file directly inside a subdirectory. My problem is that sometimes there's an additional subdirectory inside the subdirectory(!!)</p>

<p>Does anyone have any ideas how to solve this?</p>

<p>Your issue is actually those two lines, remove them and you sould be fine:</p>

<pre><code>if len(subdirList) &gt; 0:
    del subdirList[0]
</code></pre>

<p><strong>Explanation</strong> : </p>

<p>What they do is <strong>they make the first subdirectory inside each directory disappear before <code>os.walk</code> had time to walk it</strong>. So it is not surprising that you get weird behaviour regarding subdirectories.</p>

<p>Here's an illustration of that behaviour using the following tree :</p>

<pre><code>test0/
âââ test10
â   âââ test20
â   â   âââ testA
â   âââ test21
â   â   âââ testA
â   âââ testA
âââ test11
â   âââ test22
â   â   âââ testA
â   âââ test23
â   â   âââ testA
â   âââ testA
âââ testA
</code></pre>

<p><strong>Without</strong> the problematic lines:</p>

<pre><code>Found directory: ./test/test0
    testA
Found directory: ./test/test0/test10
    testA
Found directory: ./test/test0/test10/test21
    testA
Found directory: ./test/test0/test10/test20
    testA
Found directory: ./test/test0/test11
    testA
Found directory: ./test/test0/test11/test22
    testA
Found directory: ./test/test0/test11/test23
    testA
</code></pre>

<p><strong>With</strong> the problematic lines:</p>

<pre><code>Found directory: ./test/test0
    testA
Found directory: ./test/test0/test11
    testA
Found directory: ./test/test0/test11/test23
    testA
</code></pre>

<p>So we clearly see that the two subfolders <code>test10</code> and <code>test22</code> that were first in line have been ignored altogether because of the "bad lines".</p>

python glob folders subdirectories os.walk