Вопрос

I have a piece of code in Python to download files from an ftp. The code downloads the very first file in the list of available days but fails to download the second. What could be the problem?

import os, ftplib 
destdir='D:\precipitation\dl' 
ftp = ftplib.FTP('ftp.itc.nl')  
ftp.login('anonymous', '') 
ftp.cwd('pub/mpe/msg') 
available_days=['summsgmpe_20100101.zip','summsgmpe_20100102.zip', 'summsgmpe_20100103.zip', 'summsgmpe_20100104.zip', 'summsgmpe_20100105.zip', 'summsgmpe_20100106.zip', 'summsgmpe_20100107.zip', 'summsgmpe_20100108.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()
Это было полезно?

Решение

Remove your call to ftp.cwd(..)

That's moving up a directory for each iteration of the list, instead of staying in the correct folder where the files are.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top