I have this code which I was hoping would work for a list of files in a filesystem. The file names in the directory look like this:
directory/
./file-2014-7-8.info
./file-2014-7-9.info
./file-2014-7-10.info
The relevant code is this:
filetype = '.info'
dir_list = os.listdir(directory)
try:
latest_file = sorted([i for i in dir_list if i.endswith(filetype)])[-1]
return latest_file
except Exception as e:
logging.error("could not find any %s files in the directory: %s" % (filetype, e)
This code returns the 7-9.info file instead of the 7-10.info file.
How do I get it to return the 7-10 without altering the names of the files themselves? Is there an easy way?