1

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()
3
  • 1
    How does it fail? Is an exception thrown? Commented Nov 23, 2012 at 20:23
  • 2
    As an aside, the line file = available_days[available_days.index(day)] could be replaced with file = day. Better yet, just use for file in available_days: and drop the following line altogether. Commented Nov 23, 2012 at 20:25
  • ftplib.error_perm: 550 The system cannot find the file specified. Commented Nov 23, 2012 at 20:28

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.