Python:如何获取文件列表并在FTP目录中使用通配符? [英] Python: How to get list of file and use wildcard in FTP directory?

查看:740
本文介绍了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...


  1. 登录到FTP服务器

  2. 导航到所需的目录

  3. 获取文件列表,根据所需的文件格式遍历文件并进行匹配
  4. li>
  5. 下载所需文件

  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

这篇关于Python:如何获取文件列表并在FTP目录中使用通配符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆