我想通过 FTP 连接到一个地址,然后删除所有内容。目前我正在使用这段代码:
from ftplib import FTP
import shutil
import os
ftp = FTP('xxx.xxx.xxx.xxx')
ftp.login("admin", "admin")
for ftpfile in ftp.nlst():
if os.path.isdir(ftpfile)== True:
shutil.rmtree(ftpfile)
else:
os.remove(ftpfile)
我的问题是当他试图删除第一个文件时我总是得到这个错误:
os.remove(ftpfile)
WindowsError: [Error 2] The system cannot find the file specified: somefile.sys
有人知道为什么吗?
最佳答案
for something in ftp.nlst():
try:
ftp.delete(something)
except Exception:
ftp.rmd(something)
还有其他方法吗?
关于python - 连接到 FTP 后删除所有文件和文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10042838/