You could, however, use sort to ensure the order you desire. for root, dirs, files in os.walk(path): for dirname in sorted( ...
It seems to me that you could use the sort() or sorted() functions (from Stack Overflow): #
And, with benefit of learning more about Python, a different (better) way:
You can look at the source for os.walk() in Python 2.7 or in Python 3.5.
Python's built-in os.walk() is significantly slower than it needs to be, because
By default, Python will walk the directory tree in a top-down order (a
Python os.walk() Method - Python method walk() generates the file names in a directory tree by walking the tree either top-down or bottom-up.
The list is in arbitrary order, and does not include the special entries '.
The files are walked in lexical order, which makes the output deterministic but requires ...
Can I tweak os.walk() in order to produce a customized tree? How can I