get the latest file in a directory

Discussion in 'Python' started by Hank, Oct 29, 2003.

  1. Hank

    Hank Guest

    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
    Hank, Oct 29, 2003
    #1
    1. Advertising

  2. > 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
    Gustavo Niemeyer, Oct 29, 2003
    #2
    1. Advertising

  3. Hank

    Joe Francia Guest

    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().
    Joe Francia, Oct 29, 2003
    #3
  4. Hank

    Tim Golden Guest

    (Hank) wrote in message news:<>...
    > 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
    Tim Golden, Oct 30, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Damien
    Replies:
    0
    Views:
    1,554
    Damien
    Aug 4, 2005
  2. Replies:
    4
    Views:
    6,751
    Roland de Ruiter
    Jul 19, 2006
  3. Replies:
    1
    Views:
    112
    Bob Barrows [MVP]
    Jun 24, 2005
  4. Rajeswar reddy Gaulla
    Replies:
    2
    Views:
    140
  5. Mmcolli00 Mom
    Replies:
    2
    Views:
    121
    William James
    Feb 10, 2009
Loading...

Share This Page