finding the last file created in a directory

Petr Man studi4 at fe-design.de
Fri Jun 30 08:43:11 EDT 2006


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[i]
        numbers.append(item[len(dir) + len(new_file_name) + 1:])
    return new_file_name + '.' + max(numbers)


cheers

Petr



More information about the Python-list mailing list