Python:如何获取文件列表并在FTP目录中使用通配符?
问题描述:
我是python的新手。我想访问baseURL并最终获得其中一个子目录中的文件列表,以便我可以下载/解压缩此文件。具体的文件扩展名可以改变,所以我希望通过日期(yyyymmdd)找到文件的匹配。
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'
预先感谢您的指导!
到目前为止,我的代码是:
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'
答
您可以使用Python ftplib, https:// docs。 python.org/3/library/ftplib.html ,作为一个ftp客户端。我不认为通配符下载是支持的,所以你必须采取一些措施......
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...
- 登录到FTP服务器
- 导航到所需的目录
- 获取文件列表,根据所需的文件格式遍历文件并进行匹配 li>
- 下载所需文件
- Login to the FTP server
- Navigate to the desired directory
- Get a listing of the files, iterate thorough the files and match according to the desired file format
- Download desired files