get the latest file in a directory

Tim Golden tim.golden at viacom-outdoor.co.uk
Thu Oct 30 04:07:33 EST 2003


soundwave56 at yahoo.ca (Hank) wrote in message news:<731fc603.0310291321.ac3bb39 at posting.google.com>...
> Hi, 
> 
> I was wondering what is a quick way to find the latest(last modified
> date) file from a directory.
> 
> Should I use win32file.GetFileTime and compare PyTime? I don't want to
> create a PyHANDLE for every single file in the directory, not if
> there's an easier way at least.
> 
> Thanks

Given that you're talking about win32file etc. I assume you're on
Windows, so the following (slightly unorthodox) suggestion might help:

import os

files = os.popen (r"dir /od /b *.*").read ().splitlines ()
# dir /od /a-d /b # to avoid directories
files[-1]

TJG




More information about the Python-list mailing list