Skip to content

Instantly share code, notes, and snippets.

@AtmaMani
Created May 25, 2017 20:19
Show Gist options
  • Save AtmaMani/beb6d589f6b7d2a051ff14fdffa3a060 to your computer and use it in GitHub Desktop.
Save AtmaMani/beb6d589f6b7d2a051ff14fdffa3a060 to your computer and use it in GitHub Desktop.
python-ftp-file-downloader
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