I'm trying to create a conditional download from an ftp for daily files, I'm struggling to get regex to work with on file that has unique time stamps to match properly and get downloaded. Here's what I have.
Example file name:
BCW_SDP_Rolling_7Days.rpt2020-02-24-07-27-24.csv
Here's what I've been able to construct using what I know:
today = datetime.date.today()
widate = (f"{today:%Y-%m-%d}")
pattern = ("BCW_SDP_Rolling_7Days.rpt"+widate+"*.csv")
I think pull a list of files from the ftp for comparison:
ftp_list = connection.nlst()
And then want to use regex to compare against the files in that list to find the one that matches properly:
> wistring = re.search(r'"BCW_SDP_Rolling_7Days.rpt"+widate+"*.csv",
> ftp_list) filenameWI = str(wistring) print (filenameWI)
Unfortunately it either loosely matches a bunch of incorrect names, or gives an error in all the different iterations I've tried. I know there's something simple I'm missing here, please help.
Try this
Code:
import datetime
import re
filename = "BCW_SDP_Rolling_7Days.rpt2020-02-24-07-27-24.csv"
# current_date = str(datetime.date.today())
current_date = "2020-02-24"
pattern = r"BCW_SDP_Rolling_7Days\.rpt" + current_date + r"(?:-\d{2}){3}\.csv"
if re.search(pattern, filename):
print("pass")
Output:
pass