I have a dataset in a specific root and trying to iterate through dirs and files, but the topdown parameter does not work as expected.
Images
├── n01440764
│ ├── image1.jpg
│ ├─ ...
│ └── image50.jpg
└── n01443537
├── image1.jpg
├─ ...
└── image50.jpg
import os
image_dir = os.walk("Images", topdown=False)
for root, dirs, files in image_dir:
for d in dirs:
print(d)
Output:
n01443537
n01440764
Either topdown=False or True, the result is the same. But I am expecting:
n01440764
n01443537
The
topdownoption does not change the order in which directories at the same level are walked. Instead, it determines whether the (dirpath, dirnames, filenames) tuples for a directory are generated before or after the tuples for its subdirectories. In the default or True option, you can modify dirnames in place to filter out some paths and not walk them. In the False option, the subdirectories are walked first, so that sort of filtering is not possible.From the official documentation for os.walk()
As of Python 3.5, os.walk() uses os.scandir(), which returns them in an arbitrary order:
Prior to Python 3.5, it used os.listdir(), which also returned the directories in arbitrary order. See also the answer to this question: In what order does os.walk iterates iterate?
You can get the directories in the order you want by using
for dir in sorted(dirs)within your os.walk() loop.