Using ftplib to upload a file to a server....

Iwan van der Kleyn null at null.com
Fri Jun 20 20:49:00 CEST 2003


> Please excuse my "newbiness", but I can't get python to upload a file 
> using ftplib, basically I don't make sense of the transfercmd.

I use the following script myself

#!/usr/bin/env python
# Upload a file to a FTP server

from sys import argv, exit
from ftplib import FTP

if len(argv) != 6:
     print 'Incorrect number of parameters'
     print 'USAGE: upload.py <server> <username> <password> <local file> 
<remote file>'
     exit(0)

server = argv[1]
username = argv[2]
password = argv[3]
upfile = argv[5]
downfile = argv[4]

try:
     print 'Connecting to %s' % server
     ftp = FTP(server)
     ftp.login(username, password)
     print 'Connection to %s opened' % server
     #send file in binary mode to server (STOR command sets remote filename)
     ftp.storbinary('STOR %s' % upfile, open(downfile,'r'))
     ftp.quit()
     print 'File %s uploaded' % upfile
except Exception, err:
     print 'Error uploading file. Error: %s' % err





More information about the Python-list mailing list