Delete remote path using ftplib in Python

July 12, 2020 | Development Python

I had to frequently delete a remote folder, so I wrote this python script to avoid doing it manually.

import ftplib

def empty(ftp, path):
    for (name, properties) in ftp.mlsd(path=path):
        if name in ['.', '..', ".ftpquota", ".htaccess"]:
            continue
        elif properties['type'] == 'file':
            ftp.delete(f"{path}/{name}")
        elif properties['type'] == 'dir':
            empty(f"{path}/{name}")
    ftp.rmd(path)


if __name__ == "__main__":
    ftp = ftplib.FTP_TLS("host", "username", "password")
    empty(ftp, path)

~no comments
~post a comment: