On 9/6/06, Matt Williams <[EMAIL PROTECTED]> wrote:

> The input is done by specifying a directory, and using glob to find the
> ".tex" filenames.
>
> However, I want to process them so that they are arranged in the correct
> order, which means I need to sort the list of files. Of course, because
> they aren't named in any (obvious) order, I'm a bit stuck.

a clean way to do it would be to rename the files so that they can be
sorted. OTOH this involves not much python and is entirely boring.
OTOOH renaming the files might help you later on, when you need them
ordered for other purposes.

> I thought about using a dictionary to map names and order: so {"OAF":1,
> "Valuation":2...etc}, but I don't don't know how to take it on from
> here. I was thinking of looking up the filename in the dictionary (using
> .startswith() to get some basic rough-matching capacity) and then using
> that to return the order that the files should be handled in.

you can specify your own comparing funtion for aList.sort(). Here's a
comparing function, that does the same as the default:

def mysort(a, b):
    return cmp(a, b)

aList.sort(mysort)

Now you can simply lookup other values for filenames a and b and
compare that via python builtin cmp function.

You can also use a list of filenames and sort by comparing the indecees.

The bad thing is, that you have to specify all those filenames within
the script - which defeats many of the advantages to read them in
dynamically via glob ;-) Is there any chance to determine the
sortorder from the files' content?

rough-matching is another topic which might bring up some nice algorithms.

regards
Michael
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to