0

I am trying to upload a file to a remote server using Python 2.7, but when I run the script, the file on the remote FTP server is always called test_0test_ on remote FTP server, rather than test_0_13.pic.jpg as I would expect. Any ideas?

from ftplib import FTP

hosts = [('1.2.3.4', 'admin', '12345')]
local_file = r'/Users/foo/Downloads/13.pic.jpg'
remote_file_base_name_prefix = 'test_'
counter = 0
remote_file_base_name_suffix='_13.pic.jpg'

for host, name, password in hosts:
    f = FTP(host, name, password)
    f.cwd('Input')

    print remote_file_base_name_prefix+str(counter)+remote_file_base_name_suffix

    with open(local_file, 'rb') as f_local:
        f.storbinary('STOR {}'.format(remote_file_base_name_prefix+str(counter)+remote_file_base_name_prefix), f_local)

    print "{} - done".format(host)
    f.quit()

thanks in advance, Lin

2
  • 1
    I am unclear about the question - the file is always called test_0test_ on the remote server? Commented Mar 12, 2016 at 4:06
  • @mprat, yes this is what I mean. Appreciate for your ideas and vote up. :) Commented Mar 12, 2016 at 4:19

1 Answer 1

1

Ah, you just had a typo in your code:

f.storbinary('STOR {}'.format(remote_file_base_name_prefix+str(counter)+remote_file_base_name_prefix), f_local)

should be

f.storbinary('STOR {}'.format(remote_file_base_name_prefix+str(counter)+remote_file_base_name_suffix), f_local)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mprat, your solution works for me and vote up. Mark as answered as well. :)

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.