When I run os.walk()
, I get my results in alphanumeric order; starting from 0, ending at z. Is it possible to reverse this?
So if I have 3 directories; apple/
, bananas/
, pears/
, I would want pears/
, bananas/
and apples/
returned.
Obviously I could store all the dirs as a list and then .reverse()
but that's going to take a long time.
First of all, os.walk()
does not specify the order in which the directories are returned, so if I were you I wouldn't rely on the alphabetic order.
Having said that, you can choose the order in which the subdirectories are traversed by leaving topdown
set to its default value (True
), and then sorting dirs
in-place:
import os
top='/home/aix'
for root, dirs, files in os.walk(top, topdown=True):
print root
dirs.sort(reverse=True)
That'll make os.walk()
traverse the subdirectories in reverse lexicographic order of their names.
The documentation explains how this works:
When
topdown
isTrue
, the caller can modify the dirnames list in-place (perhaps usingdel
or slice assignment), andwalk()
will only recurse into the subdirectories whose names remain indirnames
; this can be used to prune the search, impose a specific order of visiting, or even to informwalk()
about directories the caller creates or renames before it resumeswalk()
again.
You cannot reverse a generator in any generic way. The only solution is to cast it to a sequence and that iterate over the sequence in reversed order. Later terms of a generator cannot necessarily be known until the earlier ones have been calculated.
The solution below uses reversed. Performance should be fine if the directory structure is not to deep.
import os
directory = '/your/dir/'
for root, dirs, files in reversed(list(os.walk(directory))):
print root, dirs, files
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With