Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > get the latest file in a directory

Reply
Thread Tools

get the latest file in a directory

 
 
Hank
Guest
Posts: n/a
 
      10-29-2003
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
 
Reply With Quote
 
 
 
 
Gustavo Niemeyer
Guest
Posts: n/a
 
      10-29-2003
> I was wondering what is a quick way to find the latest(last modified
> date) file from a directory.


>>> l = [(os.path.getmtime(x), x) for x in os.listdir(".")]
>>> l.sort()
>>> l[-1]

(1067457449, '.svn')

--
Gustavo Niemeyer
http://niemeyer.net

 
Reply With Quote
 
 
 
 
Joe Francia
Guest
Posts: n/a
 
      10-29-2003
Hank wrote:
> 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.


Look at os.stat() and the stat module, along with os.listdir().

 
Reply With Quote
 
Tim Golden
Guest
Posts: n/a
 
      10-30-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Hank) wrote in message news:<(E-Mail Removed) om>...
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
search latest version of file in directory Mmcolli00 Mom Ruby 2 02-10-2009 08:49 AM
Need ruby code to get a latest file from a Directory Rajeswar reddy Gaulla Ruby 2 08-18-2008 06:03 PM
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
Get the latest file in a directory steve.chambers@gmail.com Java 4 07-19-2006 05:40 PM
Displaying latest file in a directory maxhauser@mailcity.com ASP General 1 06-24-2005 05:26 PM



Advertisments