Reputation: 715
I was able to generate a list with my filenames:
allFiles =['https://myurl.com/something//something_01-01-2020.csv', 'https://myurl.com/something//something_01-02-2020.csv', 'https://myurl.com/something//something_01-03-2020.csv'...]
How could I find the filename with earliest date (within the file name) on this list and extract its date as variable?
Additional scenario: What if I have 00-00-0000.csv and 00-0000.csv on my list?
Upvotes: 0
Views: 138
Reputation: 13069
It seems that you want to extract the files based on the filename.
You can do:
min(allFiles, key=lambda x:x[-8:-4]+x[-11:-9]+x[-14:-12])
The lambda
function here obtains a string such as '20200101'
from the filename, by extracting the relevant parts and concatenating them in the correct order. Ordering by this string will produce date order.
This is based on the assumption that the filename adheres to a format of ending in dd-mm-yyyy.csv
. If the dates were intended in fact to be mm-dd-yyyy.csv
(not clearly specified in the question), then this would need to be changed to:
min(allFiles, key=lambda x:x[-8:-4]+x[-14:-12]x[-11:-9])
Upvotes: 0
Reputation: 8302
try this,
from datetime import datetime
min(allFiles,
key=lambda x: datetime.strptime(x.split('_')[1].replace('.csv', ''), "%d-%m-%Y"))
Upvotes: 1
Reputation: 11073
Try this:
import re
from datetime import datetime
sorted(allFiles, key= lambda x: datetime.strptime(re.search("([0-9]{2}\-[0-9]{2}\-[0-9]{4})", x)[0], '%d-%m-%Y'))
it will sort your list based on date, so you can get what you want.
the first element is earliest.
Upvotes: 0