Created
May 25, 2017 20:19
-
-
Save AtmaMani/beb6d589f6b7d2a051ff14fdffa3a060 to your computer and use it in GitHub Desktop.
python-ftp-file-downloader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ftplib | |
# connect to the server | |
ftp = ftplib.FTP('ftp.ncdc.noaa.gov') #pass the url without protocol | |
ftp.login() #pass credentials if anonymous access is not allowed | |
# switch to the directory containing the data | |
ftp.cwd('/pub/data/swdi/database-csv/v2/') | |
ftp.pwd() | |
# get the list of files in this ftp dir | |
all_files= ftp.nlst() | |
hail_files = [i for i in all_files if i.startswith('hail')] | |
# now download to the desired path | |
for filename in hail_files: | |
print("Downloading " + filename, end=" | ") | |
with open(r'E:\GIS_Data\Analytics\ncdc_hail_data\raw\\' | |
+ filename + ".csv.gz", "wb") as file_handle: | |
ftp.retrbinary("RETR " + filename, file_handle.write) | |
print(" finished") | |
# running this will print and output like below: | |
""" | |
Downloading hail-2013.csv.gz | finished | |
Downloading hail-2015.csv.gz | finished | |
Downloading hail-201704.csv.gz | finished | |
Downloading hail-2014.csv.gz | finished | |
Downloading hail-201705.csv.gz | finished | |
..... | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment