I need to download several (Digital Earth Model) zip files in a folder "C:\DEMDownload" on my PC (windows OS) from the public geodata base of Canada Government.
when i run my code at the line ftp.retrbinary('RETR %s' %file, open(local_file, 'wb').write)
i get the following error message
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Python27\lib\ftplib.py", line 414, in retrbinary
conn = self.transfercmd(cmd, rest)
File "C:\Python27\lib\ftplib.py", line 376, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Python27\lib\ftplib.py", line 339, in ntransfercmd
resp = self.sendcmd(cmd)
File "C:\Python27\lib\ftplib.py", line 249, in sendcmd
return self.getresp()
File "C:\Python27\lib\ftplib.py", line 224, in getresp
raise error_perm, resp
error_perm: 550 Failed to open file.
Second. Is It possible to avoid to write available_days list and create a list of all zip files to download
import os, ftplib
destdir='C:\DEMDownload'
ftp = ftplib.FTP('ftp2.cits.rncan.gc.ca')
ftp.login('anonymous', '')
available_days= ['001k11.zip',
'001k12.zip',
'001k13.zip',
'001k14.zip',
'001k15.zip',
'001l13.zip',
'001l14.zip',
'001l16.zip',
'001m01.zip',
'001m02.zip',
'001m03.zip',
'001m04.zip',
'001m05.zip',
'001m06.zip',
'001m07.zip',
'001m08.zip',
'001m09.zip',
'001m10.zip',
'001m11.zip',
'001m12.zip',
'001m13.zip',
'001m14.zip',
'001m15.zip',
'001m16.zip',
'001n02.zip',
'001n03.zip',
'001n04.zip',
'001n05.zip',
'001n06.zip',
'001n07.zip',
'001n10.zip',
'001n11.zip',
'001n12.zip',
'001n13.zip',
'001n14.zip',
'001n15.zip']
hdfs = list()
for day in available_days :
file = available_days[available_days.index(day)]
print 'file=', file
local_file = os.path.join(destdir, file)
ftp.retrbinary('RETR %s' %file, open(local_file, 'wb').write)
hdfs.append(os.path.abspath(local_file))
ftp.cwd('..')
ftp.quit()
I was able to successfully download the zip files with your given url with this:
You can avoid the hardcoded dir/filenames by walking through the ftp directories similar to how you might walk through a local directory with some creative usage of
ftplib.FTP.dir()
Full code below:
You can condense this further through usage of one of the python ftp wrapper libraries such as ftptool or ftputil