Process files in order

Discussion in 'Python' started by Khoa Nguyen, Jul 27, 2006.

  1. Khoa Nguyen

    Khoa Nguyen Guest

    Hi,

    I have a requirement to process all files in a directory in
    chronological order. The os.listdir() function, however, lists the
    files in random order. Is there a similar function in Python that
    allows me to specify the listing order (like ls -t for example)?

    Thanks,
    Khoa
     
    Khoa Nguyen, Jul 27, 2006
    #1
    1. Advertising

  2. Khoa Nguyen

    Yu-Xi Lim Guest

    Khoa Nguyen wrote:
    > I have a requirement to process all files in a directory in
    > chronological order. The os.listdir() function, however, lists the
    > files in random order. Is there a similar function in Python that
    > allows me to specify the listing order (like ls -t for example)?


    There is no single command, but you can easily sort the results of
    listdir using any criteria. Most file attributes can can be obtained
    using os.stat (size, creation date, modification date, etc), and you can
    just use that as a key to sort().
     
    Yu-Xi Lim, Jul 27, 2006
    #2
    1. Advertising

  3. Khoa Nguyen

    Mike Kent Guest

    How about using os.listdir to build a list of filenames, then sorting
    them by modification time (via os.stat)?
     
    Mike Kent, Jul 27, 2006
    #3
  4. Khoa Nguyen a écrit :
    > Hi,
    >
    > I have a requirement to process all files in a directory in
    > chronological order.


    Access time, modification time ? Ascending, descending ?-)

    > The os.listdir() function, however, lists the
    > files in random order. Is there a similar function in Python that
    > allows me to specify the listing order (like ls -t for example)?


    Not AFAIK. But os.path.get[acm]time(<filename>) and sorted() may help:

    from os.path import getmtime, join, isfile
    from os import listdir, getcwd

    listfiles = lambda p: filter(isfile, # only list files
    map(lambda f, p=p : join(p,f),
    listdir(p)))

    files = listfiles(getcwd())
    sortedfiles = map(lambda item: item[1],
    sorted(zip(map(getmtime, files), files)))


    You can apply reversed() to sortedfiles if you want them in reversed order.


    HTH
     
    Bruno Desthuilliers, Jul 27, 2006
    #4
  5. Khoa Nguyen

    Guest

    A possibility:

    import os
    _, _, file_names = os.walk("").next()
    print sorted(file_names, key=lambda fn: os.stat(fn)[8])

    Bye,
    bearophile
     
    , Jul 27, 2006
    #5
    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. Rithesh Pai
    Replies:
    1
    Views:
    5,629
  2. walala
    Replies:
    3
    Views:
    780
    Allan Herriman
    Sep 9, 2003
  3. Soren Kuula
    Replies:
    2
    Views:
    541
    Soren Kuula
    Feb 1, 2004
  4. cspoh
    Replies:
    0
    Views:
    267
    cspoh
    Jul 31, 2003
  5. Stephan Kämper
    Replies:
    2
    Views:
    257
    Stephan Kämper
    Jan 18, 2004
Loading...

Share This Page