pythondownloadftpnoaa

how to get only those in an FTP directory by a given name?


I would like to Download only one file group. In this case I want to download the humidity data from the NOAA server What should I do to search only the relative humidity files?

from ftplib import FTP
from datetime import datetime


print('Get Conection in NOAA server...')

start = datetime.now()
ftp = FTP('ftp.cdc.noaa.gov')
ftp.login()
ftp.cwd('Datasets/ncep.reanalysis.dailyavgs/pressure/')
print('Done!')

# Get All Files
files = ftp.nlst()
print(files)
# Print out the files

for file in files:
    print("Downloading..." + file)
    ftp.retrbinary("RETR " + file ,open("D:\\Dados_Eventos_Extremos\\Precipitacao\\" + file, 'wb').write)

print('Close Conection')
ftp.close()

end = datetime.now()
diff = start - end
print('Download Files in ' + str(diff.seconds) + 's')


# My output is this!
hgt.2017.nc
hgt.2018.nc
hgt.2019.nc
hgt.2020.nc
omega.1948.nc
omega.1949.nc
omega.1950.nc
omega.1951.nc
omega.1952.nc
omega.1953.nc
omega.1954.nc
omega.1955.nc
omega.1956.nc
omega.2020.nc
rhum.1948.nc
rhum.1949.nc
rhum.1950.nc
rhum.1951.nc
rhum.1952.nc
rhum.1953.nc
rhum.1954.nc
rhum.1955.nc
rhum.1956.nc
rhum.1957.nc
rhum.1958.nc
rhum.1959.nc
rhum.1960.nc

I would like to download only the 'rhum' files

Could someone help me, give me any ideas, please.


Solution

  • With most FTP servers, this will do:

    files = ftp.nlst("*rhum*")
    

    (although it is non-standard way).