0

When I try to use ftp.delete() from ftplib, it raises error_perm, resp:

>>> from ftplib import FTP
>>> ftp = FTP("192.168.0.22")
>>> ftp.login("user", "password")
'230 Login successful.'
>>> ftp.cwd("/Public/test/hello/will_i_be_deleted/")
'250 Directory successfully changed.'
>>> ftp.delete("/Public/test/hello/will_i_be_deleted/")
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ftplib.py", line 520, in delete
resp = self.sendcmd('DELE ' + filename)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ftplib.py", line 243, in sendcmd
return self.getresp()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ftplib.py", line 218, in getresp
raise error_perm, resp
ftplib.error_perm: 550 Delete operation failed.

The directory exists, and "user" has sufficient permissions to delete the folder.

The site is actually a NAS (WD MyBookWorld) that supports ftp.

Changing to parent directory and using command ftp.delete("will_i_be_deleted") does not work either.

"will_i_be_deleted" is an empty directory.

ftp settings for WD MyBookWorld:

Service - Enable; Enable Anonymous - No; Port (Default 21) - Default
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
jameh
  • 1,149
  • 3
  • 12
  • 20

3 Answers3

5

My solution to fix this ftplib.error_perm: 550 issue is to cwd to root directory of FTP server, and delete files by their full path as below.

ftp.cwd(‘.’)

directory = '/Public/test/hello/will_i_be_deleted/'    

# delete files in dir
files = list(ftp.nlst(directory))
for f in files:
    if f[-3:] == "/.." or f[-2:] == '/.': continue
    ftp.delete(f)

# delete this dir
ftp.rmd(directory)
ybdesire
  • 1,593
  • 1
  • 20
  • 35
2

You need to use the rmd command, i.e

ftp.rmd("/Public/test/hello/will_i_be_deleted/")

rmd is for removing directories, deleteis for removing files.

mhawke
  • 84,695
  • 9
  • 117
  • 138
  • `>>> from ftplib import FTP >>> ftp = FTP("192.168.0.22") >>> ftp.login("admin", "alisary") '230 Login successful.' >>> ftp.cwd("/Public/test/hello/") '250 Directory successfully changed.' >>> ftp.rmd("/Public/test/hello/will_i_be_deleted/")` results in `ftplib.error_perm: 550 Remove directory operation failed` – jameh May 18 '12 at 15:56
  • I also get the error when ftp.delete is performed on a .txt file – jameh May 18 '12 at 16:25
  • The FTP debug messages might provide some clues, i.e. `ftp.debug(2)` – mhawke May 20 '12 at 23:55
0

The only method that works for me is that I can rename with the ftp.rename() command:

e.g.

ftp.mkd("/Public/Trash/")
ftp.rename("/Public/test/hello/will_i_be_deleted","/Public/Trash/will_i_be_deleted")

and then to manually delete the contents of Trash from time to time.

I do not know if this is an exclusive problem for the WD MyBookWorld ftp capabilities or not, but at least I got a workaround.

jameh
  • 1,149
  • 3
  • 12
  • 20