delete() method of FTP class in Python

The delete method, removes a specified file from an FTP server. The Python example, creates an FTP instance, connects and logs in to an FTP server before deleting a file from the server.

Ftp Updated 9 Oct 2019 1 min read

Method Name

delete

Method Signature

delete(fileName)

Returns

An FTP success message like, 250 File successfully deleted or an error message like, 550 permission denied.

Exceptions

  • In case of permissions related failures it raises the exception error_perm and raises error_reply on other failures.

Overview

  • The method delete() removes a file, as specified by the parameter filename, permanently from an FTP server.

Example

python
# Example Python program that deletes a file

# from an FTP server

from ftplib import FTP

# Create an FTp instance

ftpObject = FTP();

# Connect to the host

ftpResponse = ftpObject.connect(host="ftpserver2connect");

print(ftpResponse);

# Login anonymously

ftpResponse = ftpObject.login();

print(ftpResponse);

# Change to a specific folder

ftpResponse = ftpObject.cwd("/uploadedfiles");

print(ftpResponse);

# Delete a file

ftpResponse = ftpObject.delete("ToServer.txt");

print(ftpResponse);

Output

Output
220 (vsFTPd 3.0.3)

230 Login successful.

250 Directory successfully changed.

250 File successfully deleted.