[?] sorting files by modification time.

Mike C. Fletcher mcfletch at home.com
Tue Oct 2 15:29:40 EDT 2001


PythonWin 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32.
Portions Copyright 1994-2000 Mark Hammond (MarkH at ActiveState.com) - see
'Help/About PythonWin' for further copyright information.
>>> import os, stat
>>> def sortedFiles( directory ):
... 	files = [(os.stat(os.path.join(directory, file))[-2],file) for file in
os.listdir( directory )]
... 	files.sort()
... 	return [ information[1] for information in files ]
...
>>> sortedFiles( 'c:\\bin\\w2k' )

For real-world use you'd want to turn that into a for-loop instead and catch
any exceptions arising from the os.stat calls (they can get messed up if the
file names are weird letters).

Hope that helps,
Mike


-----Original Message-----
From: python-list-admin at python.org
[mailto:python-list-admin at python.org]On Behalf Of Francisco Borges
Sent: October 2, 2001 14:37
To: python-list at python.org
Subject: [?] sorting files by modification time.



 Hello all,

  I want to list some files in a directory and sort them by the
  modification time.

 As far as I could tell, I can't do that with os.listdir and neither
 with glob, is this right?

I thought about
 >>> os.system('ls -t')

but it will thrown the output to stdout. Is there a simpler way to do
this than start messing with the stdout or with the shell???

 thank you for your attention,
--
  Francisco.
             __o
            `\<,
     _____(*)/(*)_____

--
http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list