Python: How to get list of file and use wildcard i

2020-05-09 04:35发布

问题:

I'm new to python. I want to access baseURL and eventually get a list of files in one of the sub-directories so I can download/unzip this file. The specific file extension can change so I'm hoping to find a match to the file by just the date (yyyymmdd).

baseURL = 'ftp://prism.nacse.org'

Thanks in advance for your guidance!

My code so far is:

variables  = ['ppt', 'tmax', 'tmin']
nvars = len(variables)



baseURL = 'ftp://prism.nacse.org/daily/'


stDateNum = date.toordinal(date(1981,1,1))  # Year, Month, Day
edDateNum = date.toordinal(date(2017,4,22))

dates = list(range(stDateNum,edDateNum+1))
ndates = len(dates)



for v in range(0,nvars):

    for d in range(0,ndates):

        tmpdate = date.fromordinal(dates[d]).strftime('%Y%m%d') #yyyymmdd
        tmpYR = date.fromordinal(dates[d]).strftime('%Y') #yyyy
        totalpath = baseURL + variables[v] + '/' + tmpYR + '/*_' + tmpdate + '_bil.zip'

回答1:

You could use Python ftplib, https://docs.python.org/3/library/ftplib.html, as a ftp client. I don't think wildcard downloads are support, so you'd have to do something along the lines of...

  1. Login to the FTP server
  2. Navigate to the desired directory
  3. Get a listing of the files, iterate thorough the files and match according to the desired file format
  4. Download desired files