0

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.

  • `*.` might be problematic. As I see it, you are matching the last number of whatever `widate` returns zero or more times (which is what the `*` quantifier does, it's not a wildcard), and then using a `.` (a special character, meaning any character). Could you include what exactly `widate` returns? – JvdV Feb 25 '20 at 19:02

1 Answers1

0

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
kyungmin
  • 474
  • 1
  • 4
  • 9
  • I think that's on the right track, but I'm not just trying to identify a match. I need to identify the match from "ftp_list" which is the listing of files in the ftp directory and IF file is present define a variable to the name of that specific match "FilenameWI". – Aaron Deno Feb 27 '20 at 14:45
  • I was able to solve the issue with a standard wildcard "*" without needing RegEx thankfully, but I'm keeping this in my pocket because it did accurately pull the value from the list I wanted, just wasn't the simplest solution to what I needed. – Aaron Deno Feb 27 '20 at 18:00