Lin Ma
Lin Ma

Reputation: 10139

Python FTP code sample

I need to upload the same local file into different destination file names on an FTP server. Not an expert of Python doing such function. Using Python 2.7 and any good code examples are appreciated. Thanks.

thanks in advance, Lin

Upvotes: 0

Views: 637

Answers (1)

Martin Evans
Martin Evans

Reputation: 46759

The following should get you started:

from ftplib import FTP    

hosts = [('10.1.0.1', 'abc', '123'), ('10.1.0.2', 'abc', '123')]
local_file = r'/my_folder/my_file.txt'
remote_file = 'my_file.txt'

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

    with open(local_file, 'rb') as f_local:
        f.storbinary('STOR {}'.format(remote_file), f_local)

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

This will upload my_file.txt from a single source location to each host in the hosts list. It uploads the file to the same location on each server.

Upvotes: 1

Related Questions