Sorting files
Nicola Paolucci
durdn at yahoo.it.oops!.invalid
Sun Apr 27 19:57:44 EDT 2003
Hi Zipper,
Zipper114 wrote:
> I'm a bit of a noob when it comes to Python, so this might seem a bit
> trivial, but.....
>
> How can I get the listing of files of a particular type in a directory,
> sorted by date? For example, I using the following call to get all the
> JPEG's in a dir:
>
> Images = glob.glob1(MyPath+"\\images", "*.jpg")
>
>
> I want to do just this, but have them sorted by date. Any ideas?
You could use the os.stat function and the stat module like:
import os
import pprint
import stat
p = 'f:/mp3/italiane/Franco Battiato'
tosort = []
for f in os.listdir(p):
tosort.append((os.stat(os.path.join(p,f))[stat.ST_CTIME],f))
tosort.sort()
pprint.pprint([r[1] for r in tosort])
I hope this helps,
ciao,
Nick
--
#Remove .oops!.invalid to email or feed to Python:
'Tmljb2xhIFBhb2x1Y2NpIDxuaWNrQG5vdGp1c3RjYy5jb20+'.decode('base64')
More information about the Python-list
mailing list