FTP file upload

Uploading a file to your FTP server is very simple. You can use the following code for that purpose:

import ftplib

sftp = ftplib.FTP('myserver.com','login','password') # Connect
fp = open('todo.txt','rb') # file to send
sftp.storbinary('STOR todo.txt', fp) # Send the file

fp.close() # Close file and FTP
sftp.quit()

Comments

Unknown said…
This is a perfect little nugget that helped me to accomplish exactly what I was looking to do.

Thanks!
Shantanu said…
Hey...this code is nice and concise and is what im looking for but it doesnt seem to be working on my comp....my OS is wondows 7 and im using python 2.4.3...
Shantanu said…
Im getting an error

Traceback (most recent call last):
File "WEB/python/ftp1.py", line 5, in ?
sftp.storbinary('STOR password.txt', fp) # Send the file
File "WEB/python/ftplib.py", line 415, in storbinary
conn = self.transfercmd(cmd)
File "WEB/python/ftplib.py", line 345, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "WEB/python/ftplib.py", line 327, in ntransfercmd
resp = self.sendcmd(cmd)
File "WEB/python/ftplib.py", line 241, in sendcmd
return self.getresp()
File "WEB/python/ftplib.py", line 216, in getresp
raise error_perm, resp
ftplib.error_perm: 550 Operation not complete

Popular posts from this blog

Strip HTML tags using Python

lambda magic to find prime numbers

Convert text to ASCII and ASCII to text - Python code