python从ftp下载数据保存实例
Jun 16, 2016 am 08:46 AM《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()

Article chaud

Outils chauds Tags

Article chaud

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Comment utiliser la belle soupe pour analyser HTML?

Comment utiliser Python pour trouver la distribution ZIPF d'un fichier texte

Comment travailler avec des documents PDF à l'aide de Python

Comment se cacher en utilisant Redis dans les applications Django

Comment effectuer l'apprentissage en profondeur avec TensorFlow ou Pytorch?

Comment implémenter votre propre structure de données dans Python

Sérialisation et désérialisation des objets Python: partie 1
