-1

I've used an answer to get a list of files in a directory, this looks like:

['name1_2020-06-25.csv','name1_2020-06-24.csv','name1_2020-06-23.csv','name1_2020-06-22.csv','name_2_2020-06-25.csv','name_2_2020-06-24.csv','name_2_2020-06-23.csv','name_2_2020-06-22.csv']

I would like to find a way of selecting the name1 file with the most recent date.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Abijah
  • 512
  • 4
  • 17
  • What was the problem when you tried to do it? – mkrieger1 Jun 25 '20 at 16:33
  • Does this answer your question? [Sort list of date strings](https://stackoverflow.com/questions/17627531/sort-list-of-date-strings) – mkrieger1 Jun 25 '20 at 16:34
  • No - the list contains very different elements – Abijah Jun 25 '20 at 16:39
  • Every element on the list is a filename that follows the right naming convention? or not? – Edwin Cruz Jun 25 '20 at 16:41
  • Every file on the list has a name starting with 'name1' or 'name_2' which then continues with the date in YYYY-MM-DD format, and then '.csv' if thats what you mean – Abijah Jun 25 '20 at 16:45
  • Can you tell more about the difficulty you had when trying to split these strings into a "name" part and a "date" part, and then sorting by the "date" part while ignoring those "names" you are not interested in? – mkrieger1 Jun 25 '20 at 16:52

1 Answers1

1

First, you can use the string method startswith() (docs here) to pick out the out the ones that have the right name. You do not need regex here since the names are at the beginning.

Then since the dates are structured nicely as YYYY-MM-DD you can sort the resulting list using sort() or sorted() (docs here) to get the most recent date.

Something like this:

def find_most_recent(file_list, prefix):
    s_list = sorted([fname for fname in file_list if fname.startswith(prefix)])
    return s_list[-1]

This uses list comprehension with an if clause (docs here) to create a new list filtered to be just the file names starting with the passed in prefix. Then that list is sorted by passing it to sorted().

I did not bother with reversing the sort since it is just as easy to pick off the last entry in the list (using the -1 index on the s_list), but you could if you wanted to by using the option reverse=True on sorted().

Note that startswith() would have a problem here if the prefix/name could also be a substring of another valid name, but you indicated that this was not an issue and so it could be ignored for this use case.

Glenn Mackintosh
  • 2,765
  • 1
  • 10
  • 18