finding the last file created in a directory

Discussion in 'Python' started by Petr Man, Jun 30, 2006.

  1. Petr Man

    Petr Man Guest

    Hello everyone,
    I have a repeatedly running process, which always creates a new logfile with
    an ending n+1. What I need is to find the last file, the one with highest
    number at the end. The problem is, that the max() method gives me a wrong
    answer. I tried to convert the items in my list into integers using int(),
    but that ends up with an error ValueError: invalid literal for int():

    My code currently looks like this:

    def get_logfile(dir, file_name):
    new_file_name = file_name[:-3] + 'log'
    listing = glob.glob(dir + new_file_name + '*')
    numbers = []
    for i in range(len(listing)):
    item = listing
    numbers.append(item[len(dir) + len(new_file_name) + 1:])
    return new_file_name + '.' + max(numbers)


    cheers

    Petr
     
    Petr Man, Jun 30, 2006
    #1
    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. Jack Wright
    Replies:
    1
    Views:
    489
    sonikchopra
    Apr 19, 2005
  2. Replies:
    3
    Views:
    340
    Fredrik Lundh
    Oct 30, 2006
  3. Sunfire
    Replies:
    1
    Views:
    498
    Roland Dick
    Nov 10, 2007
  4. Andyza
    Replies:
    4
    Views:
    333
    Anthony Jones
    Feb 17, 2006
  5. Ted Flethuseo

    finding last line in a file

    Ted Flethuseo, Jul 22, 2010, in forum: Ruby
    Replies:
    14
    Views:
    195
    Robert Klemme
    Jul 22, 2010
Loading...

Share This Page