FTPlib
This module defines the class FTP and a few related items. The FTP class implements the client side of the FTP protocol. You can use this to write Python programs that perform a variety of automated FTP jobs, such as mirroring other FTP servers. It is also used by the module urllib.request to handle URLs that use FTP. For more information on FTP (File Transfer Protocol), see Internet RFC 959.
- ftplib是python中自带的默认库
- 可以使用它连接、操作ftp服务器相关的内容
具体如何操作可以查看官方文档
ftplib文档
下面是使用ftplib 递归删除ftp文件夹内全部内容的代码
def delAllfile(ftp,ftppath):
try:
print (ftppath)
try:
ftp.cwd(ftppath)
except Exception as e:
print ("进入ftp目录失败" + str(e))
ftp.dir('.', dir_res.append) # 对当前目录进行dir(),将结果放入列表
print(dir_res)
for i in dir_res:
if i.startswith("d"):
dirName=i.split(" ")[-1]
print("开始删除"+dirName+"文件夹")
delAllfile(ftp,ftp.pwd() + "/" + dirName)
ftp.cwd('..')
print(ftppath+"/"+dirName)
ftp.rmd(ftppath + '/' + dirName)
else:
filelist = ftp.getfiles(ftppath)
for f in filelist:
print ("删除FTP目录:"+ftppath+"下存在文件:"+f)
ftp.delete(f)
except Exception as e:
raise e