Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.walk() in reverse?

Tags:

python

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.

like image 807
joedborg Avatar asked Jan 30 '12 10:01

joedborg


2 Answers

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 is True, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again.

like image 197
NPE Avatar answered Oct 23 '22 11:10

NPE


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
like image 34
Mariusz Jamro Avatar answered Oct 23 '22 11:10

Mariusz Jamro