paramiko sftp get error

Question:

I have the following code with initalization of credentials removed.
Printing of directory listing works, however “get” fails with the following exception

It seems to me that is failing in prefetch since I extracted the code in getfo and got it to work as a function in my code with prefetch commented out.

Is there a better solution?

*** Caught exception: <type 'exceptions.IOError'>: [Errno 2] The message [/Inbox/CD.BAIINT.D130802.T200541.M856559] is not extractable!
Traceback (most recent call last):
  File "C:ProjectsPythonSFTPSFTPHSC.py", line 71, in <module>
    sftp.get(files, localpath + "/" + files)
  File "buildbdist.win-amd64eggparamikosftp_client.py", line 676, in get
    size = self.getfo(remotepath, fl, callback)
  File "buildbdist.win-amd64eggparamikosftp_client.py", line 640, in getfo
    file_size = self.stat(remotepath).st_size
  File "buildbdist.win-amd64eggparamikosftp_client.py", line 337, in stat
    t, msg = self._request(CMD_STAT, path)
  File "buildbdist.win-amd64eggparamikosftp_client.py", line 689, in _request
    return self._read_response(num)
  File "buildbdist.win-amd64eggparamikosftp_client.py", line 736, in _read_response
    self._convert_status(msg)
  File "buildbdist.win-amd64eggparamikosftp_client.py", line 762, in _convert_status
    raise IOError(errno.ENOENT, text)
IOError: [Errno 2] The message [/Inbox/CD.BAIINT.D130802.T200541.M856559] is not extractable!

username = ''
password=''
hostname =''
port=22
localpath ="c:/BkFiles/"
t = paramiko.Transport((hostname, port))

try:
    t.connect(username=username, password=password)
    sftp = paramiko.SFTPClient.from_transport(t)

    dirlist = sftp.listdir('.')
    print "Dirlist:", dirlist

    sftp.chdir('Inbox')
    dirlist = sftp.listdir('.')
    print "Dirlist:", dirlist

    for files in dirlist:
        sftp.get(files, localpath + files)
        print files
except Exception, e:
    print '*** Caught exception: %s: %s' % (e.__class__, e)
    traceback.print_exc()
finally:
     try:
        t.close()
     except:
        pass
Asked By: erase.ego

||

Answers:

The specific error you are getting can be found in your traceback. Looking at the source code for Paramiko’s sftp_client.py:760:

elif code == SFTP_NO_SUCH_FILE:
    raise IOError(errno.ENOENT, text)

1) Apparently, you’re trying to sftp GET a file which simply doesn’t exist, or saving it to a path on the local machine that doesn’t exist. Try modifying your code to print out the paths you’re downloading and where you’re saving it to:

for files in dirlist:
    print ' -> Attempting to download: "{}", and saving it {}'.format(files, localpath + files)
    sftp.get(files, localpath + files)
print files

2) you can shorten sftp.listdir('.') to sftp.listdir() since the path’s default parameter is already '.'.

3) You may even want to print out the stat of the destination files for further debugging:

for files in dirlist:
    print ' -> Attempting to download: "{}", and saving it {}'.format(files, localpath + files)
    print ' --> remotepath stat: {}'.format(sftp.stat(files))
    sftp.get(files, localpath + files)
    print files
Answered By: VooDooNOFX

I am getting same error when I try to put a file to sftp. Even though I can put that file to my local sftp server (atmoz/sftp), it is throwing error when I try to do same to real sftp. I found this workaround: instead of uploading a file, write file’s content to sftp directly.

cnopts = pysftp.CnOpts()
cnopts.hostkeys = None

with pysftp.Connection(
            host=sftp_host,
            username=sftp_user,
            private_key=private_key_path,
            private_key_pass=sftp_password,
            port=sftp_port,
            cnopts=cnopts,
        ) as sftp:
             # sftp.put(file_path) # causes error in real sftp

             # read file's content
             with open(file_path, "rb") as f:
                file_content = f.read()

             # write file content's sftp
             with sftp.open(file_path, "wb") as f:
                f.write(file_content)
Answered By: johncasey
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.