2012-06-25 28 views
1

當我嘗試從遠程服務器獲取文件時,我遇到了paramiko的問題,它給了我IOError: [Errno 2] No such file。這裏是我的代碼:Paramiko get()如果服務器不支持df -hi會引發IOError「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發佈在命令行上(因爲我開了蟒蛇REPL相同的用戶),我可以獲取文件。有任何想法嗎?

編輯:我發現這個職位它好像我有https://bugs.launchpad.net/paramiko/+bug/492238在互動SFTP提示問題:

sftp> df -hi 
Server does not support [email protected] extension 

此錯誤是從2009年並沒有結束,(但我使用最新的paramiko 1.7.7.1)。任何人都知道解決方法?我可以強制paramiko只是做簡單的sftp get的等價物,而不嘗試檢查文件的完整性嗎?

回答

0

無論如何,這可能最終會在某個時候調用stat(),但您可以嘗試使用STFPClient.open(),它將返回SFTPFile實例。然後調用它的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() 
+0

不幸的是,這不起作用。 'open()'以及除listdir()之外的幾乎每種方法都會觸發相同的錯誤 – danny

+0

它究竟在哪裏失敗?請發佈回溯。你確定失敗在'SFTPClient.stat()'中 – mhawke

0

我有同樣的問題,但是,我沒有遇到問題,因爲sftp和df。請確保爲localpath提供正確的文件名!

sftp.get(files[2],'file2.txt') 
相關問題