Corey,
I have a bit of code that use in a CGI that sorts some picture files,
perhaps something like this will work for you.
The file sorting bit:
dir_list = os.listdir("/localhost/html/pics")
dir_list.sort() #sorts the list of filenames in place
Just in case there is something else useful in the routine I am using, here
is the total code. It not produces a sorted list of the file names, it
keeps the amount of files in the directory from growing out of control in my
particular Python CGI app. In this case, I keep only the 5 most recent
files. The code does not have to look at the file timestamps to do this
because I am using the epoch time at time of the file's creation as the
file's name i.e. from time import time; new_filename = str(int(time()))
.
file_list = ""
count = 0
dir_list = os.listdir("/localhost/html/pics")
dir_list.sort()
for file in dir_list:
count = count + 1
if count < 5:
pass
else:
basename = os.path.basename(file)
if basename.endswith('.png'):
file_list = file_list + basename+'\n'
os.remove("/localhost/html/pics/"+dir_list[0])
--Bill Allen
On Wed, Jan 12, 2011 at 6:56 PM, Corey Richardson <[email protected]> wrote:
> Hello Tutors,
>
> I am generating XML definitions for animations to be used in a
> FIFE-based game. I need to sort the frames of the animations, and I am
> currently using:
> sorted([image for image in os.listdir(path) if image.endswith('.png')])
>
> The final output in the XML is:
>
> <frame source="walk_0.png"/>
> <frame source="walk_1.png"/>
> <frame source="walk_10.png"/>
> <frame source="walk_11.png"/>
> <frame source="walk_2.png"/>
> <frame source="walk_3.png"/>
> <frame source="walk_4.png"/>
> <frame source="walk_5.png"/>
> <frame source="walk_6.png"/>
> <frame source="walk_7.png"/>
> <frame source="walk_8.png"/>
> <frame source="walk_9.png"/>
>
> Having frame 10 and 11 between frame 1 and 2 is not desired behavior;
> how can I sort all of these with walk_10.png and company (this isn't the
> only animation like this) being after walk_9.png? A google search
> doesn't return anything I could use. I tried just using
> [image for image in os.listdir(path) if image.endswith('.png')],
> but that doesn't appear to have any order.
>
> Thanks,
> Corey Richardson
> _______________________________________________
> Tutor maillist - [email protected]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor