Delete all files and folders after connecting to FTP

ftp, ftplib, python

Solution

for something in ftp.nlst():
    try:
        ftp.delete(something)
    except Exception:
        ftp.rmd(something)

Any other ways?

Problem

I want to connect through FTP to an address and then delete all contents. Currently I am using this code: ``` 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) ``` My problem is I always get this error when he is trying to delete the first file: ``` os.remove(ftpfile) WindowsError: [Error 2] The system cannot find the file specified: somefile.sys ``` Anyone has an idea why?

Original source

Related problems