SirLancillotto
SirLancillotto

Reputation: 49

Python: filename matching using regex

I have written this code

import os
from datetime import datetime
import re

now = datetime.now() 
filename = now.strftime("%Y%m%d%H%M") #For example 202006191839

for fname in os.listdir(downloadPath):                
   if re.match('export_' + filename + '[0-9]{2}.xlsx', fname):
      print(fname)

In downloadPath I have these files

But the re.match is not matching as desired.

But, if i change

filename = now.strftime("%Y%m%d%H%M")

with a simple assignment

filename = "202006191839"

The code works. The problem is, I need to have dynamic data.

Can anyone help me?

Upvotes: 0

Views: 140

Answers (2)

SirLancillotto
SirLancillotto

Reputation: 49

Ok.

I am solved the problem... i am blind probably The file I search, is download before the above code, despite being very small, the search command starts before the download... I have add a simple time.sleep(2) before search command.

Thanks to all.

Upvotes: 0

ekl1pse
ekl1pse

Reputation: 644

I think it is because you are matching 'export_' + filename, but you said the file was excel_20200619183900

Upvotes: 1

Related Questions