我遇到了 paramiko 的问题,当我尝试从远程服务器获取文件时,它给我 IOError: [Errno 2] No such file
。这是我的代码:
# 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
但是,当我在命令行上连接到 sftp 时(作为我打开 python repl 的同一用户),我可以获取文件。有什么想法吗?
编辑:我发现这篇文章似乎是我遇到的问题 https://bugs.launchpad.net/paramiko/+bug/492238在交互式 sftp 提示中:
sftp> df -hi
Server does not support statvfs@openssh.com extension
这个错误是从 2009 年开始的,并没有被解决(但我使用的是最新的 paramiko 1.7.7.1)。有人知道解决方法吗?我可以强制 paramiko 只执行与普通 sftp get 等效的操作,而不尝试检查文件完整性吗?
最佳答案
这可能最终会在某个时候调用 stat()
,但您可以尝试使用返回 SFTPFile
的 STFPClient.open()
> 实例。然后调用它的read()
方法来获取文件的内容。所以,像这样:
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()
关于python - 如果服务器不支持 df -hi,Paramiko get() 会引发 IOError "No such file",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11198364/