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
  • 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. Commented Jun 19, 2020 at 16:53
  • Yes, the file is download in the same procedure, a few seconds earlier. Commented Jun 19, 2020 at 17:08

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

No no, I am sorry, the file is export_xxxxxxxx. I was wrong to write in the post!
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.