Python download Data save instance from FTP

Source: Internet
Author: User
"The Hadoop authoritative guide" weather data can be downloaded in FTP://FTP3.NCDC.NOAA.GOV/PUB/DATA/NOAA, see this data on the Internet good happy, open ftp found a problem, ah ah, so many files ah, I went to point to save as, To point to when Ah, Thunder should have a batch download, but I did not find, it is estimated that my browser to the Thunderbolt ban, and simply write a realization of their own Python download, online early, found very simple AH

Copy the Code code as follows:


#!/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, show details
Ftp.connect (ftp_server,21) #连接
Ftp.login (Username,password) #登录, if anonymous login is replaced by an empty string
return FTP

Def downloadfile ():
FTP = Ftpconnect ()
#print ftp.getwelcome () #显示ftp服务器欢迎信息
DataPath = "/pub/data/noaa/"
year=1911
While year<=1930:
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# put the date on the front, easy to sort
BufSize = 1024x768 #设置缓冲块大小
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 ()

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.