Python os.walk topdown true with regular expression
Question: I am confused as to why the following ONLY works with `topdown=False` and
returns nothing when set to `True` ?

The reason I want to use `topdown=True` is because it is taking a very long
time to traverse through the directories. I believe that going topdown will
increase the time taken to produce the list.

    
    
    for root, dirs, files in os.walk(mypath, topdown=False): #Why doesn't this work with True?
        dirs[:] = [d for d in dirs if re.match('[DMP]\\d{8}$', d)]
            for dir in dirs:
                print(dir)
    


Answer: In your code you were looking for matching names([dmp]\d{8}) to traverse into,
while you should be looking for non-matching directories to traverse into
while adding matching names to a global list.

I modified your code and this works:

    
    
    import os
    import re
    
    all_dirs = []
    for root, dirs, files in os.walk("root", topdown=True):
        subset = []
        for d in dirs:
            if not re.match('[dmp]\d{8}$', d):
                # step inside
                subset.append(d)
            else:
                # add to list
                all_dirs.append(os.path.join(root, d))
        dirs[:] = subset
    
    print all_dirs
    

This returns:

> ['root/temp1/myfiles/d12345678',  
>  'root/temp1/myfiles/m11111111',  
>  'root/temp2/mydirs/moredirs/m22222222',  
>  'root/temp2/mydirs/moredirs/p00000001']

