Paramiko get() raises IOError “No such file” if server doesn't support df -hi

帅比萌擦擦* 提交于 2019-12-12 17:51:29

问题


I'm running into a problem with paramiko where it gives me IOError: [Errno 2] No such file when I try to get a file off the remote server. Here's my code:

# set up a transport object t (using an rsa key), which connected successfully
>>> t.is_active()
True
>>> sftp = paramiko.SFTPClient.from_transport(t)
>>> files = sftp.listdir()  # files holds the list ['canceled', 'downloaded', 'FILE.06222012.TXT']
>>> sftp.get(files[2], '.')
IOError: [Errno 2] No such file

However, when I connect to sftp on the command line (as the same user I opened the python repl with) I can get the file. Any ideas?

EDIT: I found this post which seems like the issue I'm having https://bugs.launchpad.net/paramiko/+bug/492238 In the interactive sftp prompt:

sftp> df -hi
Server does not support statvfs@openssh.com extension

This bug is from 2009 and wasn't closed out (but I'm using the latest paramiko 1.7.7.1). Anyone know of a workaround? Can I force paramiko to just do the equivalent of plain sftp get, without trying to check file integrity?


回答1:


This might end up calling stat() at some point anyway, but you could try using STFPClient.open() which returns a SFTPFile instance. Then call its read() method to get the contents of the file. So, something like this:

sftp = paramiko.SFTPClient.from_transport(t)
files = sftp.listdir()  # files holds the list ['canceled', 'downloaded', 'FILE.06222012.TXT']
remote_file = sftp.open(files[2])
with open(files[2], 'w') as local_file:
    local_file.write(remote_file.read())
remote_file.close()



回答2:


I had the same issues, however, I did not run into problems because of sftp and df. Make sure to give a proper filename for localpath!

sftp.get(files[2],'file2.txt')


来源:https://stackoverflow.com/questions/11198364/paramiko-get-raises-ioerror-no-such-file-if-server-doesnt-support-df-hi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!