jameh
jameh

Reputation: 1259

FTP.delete(filename) from ftplib error

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

Upvotes: 0

Views: 22898

Answers (3)

ybdesire
ybdesire

Reputation: 1683

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)

Upvotes: 6

jameh
jameh

Reputation: 1259

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.

Upvotes: 0

mhawke
mhawke

Reputation: 87084

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.

Upvotes: 2

Related Questions