Monisha
Monisha

Reputation: 31

How to match filename containg datetime values in python

I have got a file with name as 'Entry_Form_25012022_121314' where 25012022 is date in ddmmyyyy format and 121314 is time in hhmmss format.

I need to check if any input file matches this format. The date and time would change for the inputted file however it should follow the same pattern.

I tried using fnmatchcase as below. However this will take any file without the date time patterns too.

import fnmatch

if fnmatchcase('Entry_Form_24012022','Entry_Form_*'):

Print('correct')

Upvotes: 0

Views: 110

Answers (3)

Gui Wenjia
Gui Wenjia

Reputation: 1

you can use re module to check if the filename is right or not.

Upvotes: 0

Tirth Bhagwat
Tirth Bhagwat

Reputation: 28

You can do it using slicing and using the datetime module to check if the given date/time is valid or not.

import datetime

f_name = 'Entry_Form_25012022_121314'
isValid = True
if f_name.startswith('Entry_Form_'):

    if f_name[-6:].isnumeric():
        try:
            t = f_name[-6:]
            datetime.time(hour=int(t[:2]), minute=int(t[2:4]), second=int(t[4:]))
        except ValueError:
            isValid=False
    else:
        isValid=False

    if f_name[-7] != '_':
        isValid=False      

    if f_name[-15:-7].isnumeric():
        try:
            dt = f_name[-15:-7]
            datetime.date(day=int(dt[:2]), month=int(dt[2:4]), year=int(dt[4:]))
        except ValueError:
            isValid=False
    else:
        isValid=False            
else:
    isValid=False

Upvotes: 1

PythonPro
PythonPro

Reputation: 291

According to the fnmatch documentation, [seq] can be used to match specific characters. In your case, you would want a pattern like this:

import fnmatch

if fmmatchcase(filename, 'Entry_Form_[0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789][0123456789]_[0123456789][0123456789][0123456789][0123456789][0123456789][0123456789]'):
    # Filename matches

However, this solution seems unnecessarily verbose. If you just want to match a string with a pattern, you can use regexes with the re module, which is also built in.

import re

# This creates the regex for later use.
# It helps with speed if you need to use a regex multiple times.
# If you only need to use a regex once, you can use re.match(pattern, string) directly.
# Backslashes in regexes should be escaped. Otherwise, they may cause an error.
filename_regex = re.compile('^Entry_Form_\\d{8}_\\d{6}$')

# Regex explanation:
# ^                            This matches the beginning of the string
#  Entry_Form_                 This matches the literal string "Entry_Form_"
#             \d{8}            This matches any digit (0-9) exactly eight times
#                  _           This matches the underscore that separates the date and the time
#                   \d{6}      This matches the time
#                        $     This matches the end of the string

if filename_regex.match(filename):
    # Filename matches

Upvotes: 2

Related Questions