简体   繁体   中英

paramiko sftp can't delete remote folder, ioerror

This is my code, to delete a remote directory using paramiko sftp.

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)

Execute above code will output this error,

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

This is not the case when I'm using sftp.remove(path) for a single file. But sftp.rmdir causing IOError

The syntax is from the documentation .

The error is because the destination directory has got files inside it. Try recurssive delete instead.. See below..

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM