Here are different files and the output which should look like:
- abc.csv – Correct
- abc.csv.gz – Fail
- abc.csv.csv – Fail
- abc.def.csv – Correct
The present regex looks like .*[.]csv$ which passes cases 1 and 2, but not 3 and 4. I tried using {} and \B to allow only 1 extension but wasn’t able to make it correctly.
>Solution :
As said in the comments, filename.endswith('.csv') is in my opinion the most pythonic option.
That said, if you want to use a regex, you can use re.match('.*(?<!\.csv)(\.csv)$', s) or re.fullmatch('.*(?<!\.csv)(\.csv)', s)
s = 'abc.csv.csv'
re.match('.*(?<!\.csv)(\.csv)$', s)
# no match
s = 'abc.def.csv'
re.match('.*(?<!\.csv)(\.csv)$', s)
#<re.Match object; span=(0, 11), match='abc.def.csv'>