os.walk topdown=False is not merely the reverse of topdown=True
Given this directory tree:
demo
+-- a.txt
+-- b.txt
+-- dir1
> +-- c.txt
+-- dir2
+-- dir3
> +-- e.txt
+-- d.txt
os.walk('demo', topdown=True) yields items:
('demo', ['dir2', 'dir1'], ['b.txt', 'a.txt'])
('demo/dir2', ['dir3'], ['d.txt'])
('demo/dir2/dir3', [], ['e.txt'])
('demo/dir1', [], ['c.txt'])
and topdown=False yields:
('demo/dir2/dir3', [], ['e.txt'])
('demo/dir2', ['dir3'], ['d.txt'])
('demo/dir1', [], ['c.txt'])
('demo', ['dir2', 'dir1'], ['b.txt', 'a.txt'])
(The specific order that subdirectories are visited may depend on the
order you created them, and the implementation details of the file
system you are using.)
I don’t find the documentation to os.walk clear, but I think that we
can translate it into standard tree traversal modes:
topdown = True:
- visit the current node;
- visit the node’s children.
topdown = False:
- visit the node’s children;
- visit the current node.