> 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():
The int() call should be passed only strings that are numeric...if you've got other bits of your filename in there (such as, in your code, the terminal period), it will likely choke on them with the above-mentioned ValueError. Presuming your filenames are of the form "fileXXXX.log" (adjustable by altering the 'filename' and 'suffix' variables) >>> filename = 'file' >>> suffix = '.log' >>> # create ourselves some sample filenames as one >>> # might get from globbing >>> filenames = ['%s%s%s' % (filename, i, suffix) for i in range(1,30, 7)] >>> filenames ['file1.log', 'file8.log', 'file15.log', 'file22.log', 'file29.log'] >>> # extract the maximum number from the list of file numbers: >>> max_file_number = max([int(s[len(filename):-len(suffix)]) for s in filenames]) >>> max_file_number 29 As you can see, it handles disjoint spans of file numbers, so even if you delete items in the middle, it should still find the max. There is a race condition in this in the event that you have multiple items attempting to write the file-name at hand. Thus, between the time you get the max_file_number+1, and you open the file, some other process might have created a file with that number. Use with caution accordingly. -tkc -- http://mail.python.org/mailman/listinfo/python-list