finding the last file created in a directory

P

Petr Man

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top