-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.

6
  • What was the problem when you tried to do it? Commented Jun 25, 2020 at 16:33
  • Does this answer your question? Sort list of date strings Commented Jun 25, 2020 at 16:34
  • No - the list contains very different elements Commented Jun 25, 2020 at 16:39
  • Every element on the list is a filename that follows the right naming convention? or not? Commented Jun 25, 2020 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 Commented Jun 25, 2020 at 16:45

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.