I have a folder with some files inside called in this way:
JavaScript
x
5
1
file_1_20200101235900
2
file_2_20200101235900
3
file_3_20200101235900
4
file_4_20200109999999
5
I would like to get a list containing all the files that have in their name a date in the format 'YYYYMMDDHHMMSS'
. so in this example I would like to get all the files except file_4_20200109999999
because the date in it does not exist.
expected output:
list =[file_1_20200101235900, file_2_20200101235900, file_3_20200101235900]
Advertisement
Answer
You can use os.listdir
to iterate over the files and a custom function based on datetime.datetime
to validate the date.
Using a real date parser is the best option to ensure accounting for all subtleties to determine whether or not a date is valid.
JavaScript
1
13
13
1
def valid_date(date, fmt='%Y%m%d%H%M%S'):
2
from datetime import datetime
3
try:
4
datetime.strptime(date, fmt)
5
return True
6
except ValueError:
7
return False
8
9
import os
10
11
path = '/path/to/your/files/'
12
files = [f for f in os.listdir(path) if valid_date(f.rpartition('_')[-1])]
13
output:
JavaScript
1
2
1
['file_2_20200101235900', 'file_3_20200101235900', 'file_1_20200101235900']
2