St4rb0y
St4rb0y

Reputation: 309

Python ftplib.error_perm 550: No such file or directory?

I've written a Python script that is part of my attempt to automate daily ftp transfers from my server. I've tested the script with a number of files and file types (html, mp3, png, jpg, etc.) and everything seems to work out fine so far. However, when I try to download a simple text file, 'file.txt' (9 kb), the download fails, although I account for text files and switch from binary to text mode for the transfer. The following exception is thrown by ftplib:

ftplib.error_perm: 550 file.txt: No such file or directory

Here's my script:

from ftplib import FTP_TLS, error_perm
import os


def open_connection(server, user, pwd, work_dir=None):
    global ftps
    try:
        ftps = FTP_TLS(host=server)
        ftps.login(user=user, passwd=pwd)
        ftps.prot_p() # switch to secure data connection
        if work_dir != None:
           ftps.cwd(work_dir)
        else:
            pass
    except:
        pass    


def download_file(remote_path, local_path):
    remote_file = os.path.basename(remote_path)
    local_file_path = os.path.join(local_path, remote_file)

    # differentiate between text and binary files
    file_type, encoding = guess_type_and_encoding(remote_file)

    # possibly needs a permission exception catch
    if file_type.split("/")[0] == "text" and encoding == None:
        # use text mode for transfer
        local_file = open(local_file_path, 'w')
        def callback(line): local_file.write(line + "\n")
        ftps.retrlines("RETR " + remote_file, callback)
        local_file.close()

    else:
        # use binary mode for transfer
        local_file = open(local_file_path, 'wb')
        ftps.retrbinary("RETR " + remote_file, local_file.write)
        local_file.close()
    return


def guess_type_and_encoding(filename):
    from mimetypes import guess_type, add_type
    add_type('text/x-python-win', '.pyw') # not in tables
    mimetype, encoding = guess_type(filename, False) # allow extras
    mimetype = mimetype or "?/?" # type unknown
    return mimetype, encoding


open_connection(server, user, pwd, work_dir)
download_file("/files/dir/file.txt", "/Users/username/Desktop")
ftps.close()

I don't get why the error is raised!? The arguments 'remote_path' and 'local_path' are correctly provided. Both paths exist! 'file.txt' exists on the server under /files/dir and /Users/username/Desktop points to my desktop on OS X.

Here's the detailed ftplib error:

Traceback (most recent call last):
    File "ftp2.py", line 138, in <module>
        download_file("/files/dir/file.txt", "/Users/username/Desktop")
    File "ftp2.py", line 93, in download_file
        ftps.retrlines("RETR " + remote_file, callback)
    File  "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 735, in retrlines
        conn = self.transfercmd(cmd)
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 376, in transfercmd
        return self.ntransfercmd(cmd, rest)[0]
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 710, in ntransfercmd
        conn, size = FTP.ntransfercmd(self, cmd, rest)
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 339, in ntransfercmd
        resp = self.sendcmd(cmd)
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 249, in sendcmd
        return self.getresp()
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 224, in getresp
        raise error_perm, resp
ftplib.error_perm: 550 file.txt: No such file or directory

Any help is greatly appreciated. Thanks. :)

Upvotes: 4

Views: 12115

Answers (1)

Shawn_huang
Shawn_huang

Reputation: 26

Try to replace remote_file in ftps.retrlines("RETR " + remote_file, callback) with remote_path.

Upvotes: 1

Related Questions