paramiko sftp得到错误

时间:2013-08-03 01:45:31

标签: python sftp paramiko

我有以下代码,删除了凭据的初始化。 打印目录列表工作,但“get”失败,出现以下异常

在我看来,由于我在getfo中解压缩了代码,并且在我的代码中将其作为函数工作,并且预取了注释,因此在预取中失败了。

有更好的解决方案吗?

*** 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:\Projects\Python\SFTP\SFTPHSC.py", line 71, in <module>
    sftp.get(files, localpath + "/" + files)
  File "build\bdist.win-amd64\egg\paramiko\sftp_client.py", line 676, in get
    size = self.getfo(remotepath, fl, callback)
  File "build\bdist.win-amd64\egg\paramiko\sftp_client.py", line 640, in getfo
    file_size = self.stat(remotepath).st_size
  File "build\bdist.win-amd64\egg\paramiko\sftp_client.py", line 337, in stat
    t, msg = self._request(CMD_STAT, path)
  File "build\bdist.win-amd64\egg\paramiko\sftp_client.py", line 689, in _request
    return self._read_response(num)
  File "build\bdist.win-amd64\egg\paramiko\sftp_client.py", line 736, in _read_response
    self._convert_status(msg)
  File "build\bdist.win-amd64\egg\paramiko\sftp_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

1 个答案:

答案 0 :(得分:2)

您可以在追溯中找到您遇到的具体错误。查看Paramiko的sftp_client.py:760的源代码:

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

1)显然,你试图sftp获取一个根本不存在的文件,或者将它保存到本地机器上不存在的路径上。尝试修改代码以打印出您正在下载的路径以及将其保存到的位置:

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

2)您可以将sftp.listdir('.')缩短为sftp.listdir(),因为路径的默认参数已经是'.'

3)您甚至可能希望打印出目标文件的统计信息以进行进一步调试:

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