Home  >  Article  >  Backend Development  >  python从ftp下载数据保存实例

python从ftp下载数据保存实例

WBOY
WBOYOriginal
2016-06-16 08:46:191129browse

《hadoop权威指南》的天气数据可以在ftp://ftp3.ncdc.noaa.gov/pub/data/noaa下载,在网上看到这个数据好开心,打开ftp发现个问题,呀呀,这么多文件啊,我一个个去点另存为,得点到啥时候啊,迅雷应该有批量下载,只是我没找到,估计是我浏览器把迅雷禁掉了,干脆自己用python写一个实现下载好了,网上早了一下,发现很简单啊

复制代码 代码如下:

#!/usr/bin/python
#-*- coding: utf-8 -*-

from ftplib import FTP

def ftpconnect():
    ftp_server = 'ftp3.ncdc.noaa.gov'
    username = ''
    password = ''
    ftp=FTP()
    ftp.set_debuglevel(2) #打开调试级别2,显示详细信息
    ftp.connect(ftp_server,21) #连接
    ftp.login(username,password) #登录,如果匿名登录则用空串代替即可
    return ftp

def downloadfile(): 
    ftp = ftpconnect()   
    #print ftp.getwelcome() #显示ftp服务器欢迎信息
    datapath = "/pub/data/noaa/"
    year=1911
    while year        path=datapath+str(year)
        li = ftp.nlst(path)
        for eachFile in li:
            localpaths = eachFile.split("/")
            localpath = localpaths[len(localpaths)-1]
            localpath='weatherdata/'+str(year)+'--'+localpath#把日期放在最前面,方便排序
            bufsize = 1024 #设置缓冲块大小     
            fp = open(localpath,'wb') #以写模式在本地打开文件
            ftp.retrbinary('RETR ' + eachFile,fp.write,bufsize) #接收服务器上文件并写入本地文件
        year=year+1
    ftp.set_debuglevel(0) #关闭调试
    fp.close()
    ftp.quit() #退出ftp服务器


if __name__=="__main__":
    downloadfile()

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn