Skip to content
Advertisement

Get the list of files inside a folder that have a date inside their name

I have a folder with some files inside called in this way:

file_1_20200101235900
file_2_20200101235900
file_3_20200101235900
file_4_20200109999999

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.

def valid_date(date, fmt='%Y%m%d%H%M%S'):
    from datetime import datetime
    try:
        datetime.strptime(date, fmt)
        return True
    except ValueError:
        return False
    
import os

path = '/path/to/your/files/'
files = [f for f in os.listdir(path) if valid_date(f.rpartition('_')[-1])]

output:

['file_2_20200101235900', 'file_3_20200101235900', 'file_1_20200101235900']
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement