来自ftplib错误的FTP.delete(filename)

时间:2012-05-18 05:13:19

标签: python networking ftp delete-file ftplib

当我尝试使用来自ftplib的ftp.delete()时,它会引发error_perm,而不是:

>>> 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.

目录存在,“user”具有足够的权限来删除文件夹。

该网站实际上是支持ftp的NAS(WD MyBookWorld)。

更改为父目录并使用命令ftp.delete("will_i_be_deleted")也不起作用。

“will_i_be_deleted”是一个空目录。

WD MyBookWorld的

ftp设置:

Service - Enable; Enable Anonymous - No; Port (Default 21) - Default

3 个答案:

答案 0 :(得分:2)

您需要使用rmd命令,即

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

rmd用于删除目录,delete用于删除文件。

答案 1 :(得分:2)

我修复此ftplib.error_perm:550问题的解决方案是cwd到FTP服务器的根目录,并按照完整路径删除文件,如下所示。

ftp.cwd(‘.’)

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

# delete files in dir
files = list(ftp.nlst(directory))
for f in files:
    ftp.delete(f)

# delete this dir
ftp.rmd(directory)

答案 2 :(得分:0)

对我有用的唯一方法是我可以使用ftp.rename()命令重命名:

e.g。

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

然后手动删除Trash的内容。

我不知道这是否是WD MyBookWorld ftp功能的独家问题,但至少我得到了解决方法。