Simple sort for lists?
Raymond Hettinger
vze4rx4y at verizon.net
Wed Nov 20 18:23:36 EST 2002
"R. Arens" <martian_bob at portalofevil.com>
> Hi, I'm using os.path.walk to walk through directories and join
> ordered files in the directories into one large file. Problem is,
> os.path.walk doesn't process the files in order, due to the way
> os.path.walk sends the filenames to the visit function. Does Python
> provide a method to sort the list of filenames provided by
> os.path.walk?
The code for os.path.walk is short, sweet, and easily copied so
you can roll your own version like this:
def walk(top, func, arg):
try:
names = os.listdir(top)
names.sort() ## this is the one change
except os.error:
return
func(arg, top, names)
exceptions = ('.', '..')
for name in names:
if name not in exceptions:
name = join(top, name)
if isdir(name):
walk(name, func, arg)
Raymond Hettinger
More information about the Python-list
mailing list