Sort files by date

F

fargo

Hi.

I'm looking for some way to sort files by date.

I'm usin glob module to list a directiry, but files are sorted by name.
.... print ScannedFile

I googled my problem, but did not find any solution, neither in this
newsgroup.

Can anyone help me ?

Thanks
 
J

Jeremy Sanders

fargo said:
I'm looking for some way to sort files by date.

you could do something like:

l = [(os.stat(i).st_mtime, i) for i in glob.glob('*')]
l.sort()
files = [i[1] for i in l]

Jeremy
 
F

fargo

Jeremy said:
you could do something like:

l = [(os.stat(i).st_mtime, i) for i in glob.glob('*')]
l.sort()
files = [i[1] for i in l]

Thank you for your help, this is excatly what I wasa looking for.
 
S

Scott David Daniels

Jeremy said:
fargo wrote:

I'm looking for some way to sort files by date.


you could do something like:

l = [(os.stat(i).st_mtime, i) for i in glob.glob('*')]
l.sort()
files = [i[1] for i in l]

Jeremy
If you have 2.4 or later:

def mtime(filename):
return os.stat(filename).st_mtime

lst = sorted(glob.glob('*'), key=mtime)



--Scott David Daniels
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,261
Messages
2,571,308
Members
47,968
Latest member
SerenaRusc

Latest Threads

Top