-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnoaa_dataset.py
43 lines (39 loc) · 1.21 KB
/
noaa_dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from ftplib import FTP
import os
def ftp_connect():
ftp_server = 'ftp.ncdc.noaa.gov'
username = ''
password = ''
ftp = FTP()
ftp.set_debuglevel(1)
ftp.connect(ftp_server, 21)
ftp.login(username, password)
return ftp
def download_file():
ftp = ftp_connect()
data_path = '/pub/data/noaa/'
begin_year = int(input('输入起始年份:'))
end_yaer = int(input('输入结束年份:'))
while begin_year <= end_yaer:
path = data_path + str(begin_year)
data_list = ftp.nlst(path)
if os.path.exists('noaa') is True:
os.mkdir('noaa/' + str(begin_year) + '/')
else:
os.mkdir('noaa')
os.mkdir('noaa/' + str(begin_year) + '/')
for eachfile in data_list:
localpaths = eachfile.split('/')
localpath = localpaths[len(localpaths) - 1]
localpath = 'noaa/' + str(begin_year) + '/' + localpath
bufsize = 1024
fp = open(localpath, 'wb')
ftp.retrbinary('RETR ' + eachfile, fp.write, bufsize)
begin_year = begin_year + 1
ftp.set_debuglevel(0)
fp.close()
ftp.quit()
def main():
download_file()
if __name__ == "__main__":
main()