0

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

  • export_20200619183900.xlsx
  • export_20200619183921.xlsx
  • export_20200619183930.xlsx

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?

  • 2
    Errrr, `datetime.now()` gives your current, local time. Are you sure you have a file with this timestamp? It will change every time you run your script. – bavaza Jun 19 '20 at 16:53
  • Yes, the file is download in the same procedure, a few seconds earlier. – SirLancillotto Jun 19 '20 at 17:08

2 Answers2

1

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

Vthechamp
  • 636
  • 1
  • 8
  • 20
0

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.