traversing folders, several subfolders for the files in python
Question: I've a folder structure similar to what's outlined below.

    
    
    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
    

I want to be able to list the first FileA and first FileB for each SubDir1 and
SubDir2

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

    
    
    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) > 0:
            del subdirList[0
    

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(!!)

Does anyone have any ideas how to solve this?


Answer: Your issue is actually those two lines, remove them and you sould be fine:

    
    
    if len(subdirList) > 0:
        del subdirList[0]
    

**Explanation** :

What they do is **they make the first subdirectory inside each directory
disappear before`os.walk` had time to walk it**. So it is not surprising that
you get weird behaviour regarding subdirectories.

Here's an illustration of that behaviour using the following tree :

    
    
    test0/
    ├── test10
    │   ├── test20
    │   │   └── testA
    │   ├── test21
    │   │   └── testA
    │   └── testA
    ├── test11
    │   ├── test22
    │   │   └── testA
    │   ├── test23
    │   │   └── testA
    │   └── testA
    └── testA
    

**Without** the problematic lines:

    
    
    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
    

**With** the problematic lines:

    
    
    Found directory: ./test/test0
        testA
    Found directory: ./test/test0/test11
        testA
    Found directory: ./test/test0/test11/test23
        testA
    

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

