sorting directory

Bengt Richter bokr at oz.net
Sun May 25 20:23:59 EDT 2003


On 25 May 2003 16:18:21 -0700, psybar_phreak at yahoo.com (psybar phreak) wrote:

>i am currently using glob (thanks to the wisdom of yee here) to filter
>a directory for files that end in certain extensions, as i get a
>match, i print out the contents.  what i want to do now, however, is
>to sort these according to the file name, eg file1, comes before file
>2.
>
>can i sort the directory contents, before i start filtering? or am i
>going to have to filter, put results in a list, then sort, then loop
>through list and print results?  (i hope its the first one!)
>
well, even the second is just

    import glob
    path_list = glob.glob(path_pattern)
    path_list.sort()
    for path in path_list: print path

for windows NT4, Pythoh 2.2.2 glob.glob seems to return a list that is sorted already, so

    for path in glob.glob(path_pattern): print path

should do it.

I see that Python 1.5.2 glob on slackware linux doesn't sort, but is it a real problem?

>thanks!!

De nada.

Regards,
Bengt Richter




More information about the Python-list mailing list