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)
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:
This returns: