tags:

views:

61

answers:

3

i'm writing an application that is supposed to upload a file to an FTP server.

Here's the code:

    try:
        f = open(filename,"rb")
    except:
        print "error 0"
    try:
        ftp = FTP(str(self.ConfigUri))
        print "CONNECTED!"
    except:
        print "CANNOT CONNECT"
    try:
        ftp = FTP(str(self.ConfigUri))   # connect to host, default port
    except:
        print "error 1"
    try:
        ftp.login()               # user anonymous, passwd anonymous@
    except:
        print "error2"
    try:
        ftp.storbinary('STOR ' + filename, f)
    except:
        print "error 3"
    try:
        ftp.quit()
    except:
        print "error 4"

I am getting an error at ftp.storbinary('STOR ' + filename, f). Any ideas why?

A: 

It could be that the filename is the full path, you should use the basename instead:

import os
folder, base = os.path.split(filename)
ftp.storbinary('STOR ' + base, f)

If not make sure your python is running at the right place:

import os
print os.getcwd()
Sacha
A: 

What error does the following code report?

try:
    with open(filename,"rb") as f:
        ftp = FTP(str(self.ConfigUri))   # connect to host, default port
        ftp.login()               # user anonymous, passwd anonymous@
        ftp.storbinary('STOR ' + filename, f)
        ftp.quit()
except Exception, e:
    print e

EDIT: Error 550? Looks like access denied error... Does anonymous user have the rights to write into that FTP directory?

eumiro
No such fie or directory
Juli
filename is doc.txt
Juli
does the file doc.txt exist in the current directory?
eumiro
yes, in the local machine current directory
Juli
@Juli: edit your question, with the actual traceback.
SilentGhost
550: No such file or directory
Juli
@Juli: post actual value of the `filename`
SilentGhost
@Juli: try to `print "<%s>" % filename` just between the login and storbinary and then (still between login and storbinary) `print "<%s>" % ftp.pwd()`
eumiro
@Juli: Error 550? Looks like access denied error... Does anonymous user have the rights to write into that FTP directory?
eumiro
print "<%s>" % filename: doc.txt
Juli
print "<%s>" % ftp.pwd(): </>
Juli
A: 

It looks like you are passing a full path to open the file and you are using that same full path to name the file for ftplib. Instead, cd to that directory and name the file with just the file name.

Stealing @eumiro's code:

from ftplib import FTP
import os.path

try:
    with open(fullpath,"rb") as f:
        directory, filename = os.path.split(fullpath)
        ftp = FTP(str(self.ConfigUri))   # connect to host, default port
        ftp.login()               # user anonymous, passwd anonymous@
        ftp.cwd(directory)
        ftp.storbinary('STOR ' + filename, f)
        ftp.quit()
except Exception, e:
    print e

If the directory is not on the server, you can make it:

ftp.mkd(directory)

Or you can omit the ftp.cwd() call and just place the file in the ftp root.

hughdbrown
`directory` is a local directory
SilentGhost
@SilentGhost: I got that. It's possible that the server has the same directory structure or the OP wants to create that. Placing the file in the ftp root would be the most direct test. (My suggestion to split the directory and filename was based on the untested suspicion that ftp.storbinary() does not take paths at all.) Who will be the first to be upvoted on this question? Woooo, the anticipation!
hughdbrown