2016-02-19 79 views
1

這是我的代碼,使用paramiko sftp刪除遠程目錄。paramiko sftp無法刪除遠程文件夾,ioerror

import paramiko 

host = "192.168.1.13" 
port = 22 
transport = paramiko.Transport((host, port)) 

username = "root" 
password = "abc123" 

transport.connect(username = username, password = password) 

sftp = paramiko.SFTPClient.from_transport(transport) 

filepath = '/root/test_folder' 
sftp.rmdir(filepath) 

執行上面的代碼將輸出這個錯誤,

Traceback (most recent call last): 
    File "autom_test.py", line 36, in <module> 
    sftp.rmdir(filepath) 
    File "/usr/lib/python2.7/site-packages/paramiko/sftp_client.py", line 390, in rmdir 
    self._request(CMD_RMDIR, path) 
    File "/usr/lib/python2.7/site-packages/paramiko/sftp_client.py", line 729, in _request 
    return self._read_response(num) 
    File "/usr/lib/python2.7/site-packages/paramiko/sftp_client.py", line 776, in _read_response 
    self._convert_status(msg) 
    File "/usr/lib/python2.7/site-packages/paramiko/sftp_client.py", line 806, in _convert_status 
    raise IOError(text) 
IOError: Failure 

這不是這種情況時,我使用sftp.remove(path)爲單個文件。但是sftp.rmdir造成IOError

語法是從the documentation

回答

1

錯誤是因爲目標目錄裏面有文件。 嘗試recurssive刪除,而不是..見下文..

ssh = paramiko.SSHClient() 
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 

ssh.connect(host,username=username,password=password) 

filepath="/root/test_folder" 
cmd = "rm -rf " + filepath 

stdin, stdout, stderr = ssh.exec_command(cmd) 
while not stdout.channel.exit_status_ready(): 
    time.sleep(5) 
+0

allrite感謝您的想法。我已經改變,而不是paramiko我發送bash命令來解決問題 –