Rename() Method Of FTP Class In Python

The rename() method of the FTP class from the python standard library module ftplib renames a file in the FTP server. The Python example program logs in to the FTP server and changes the current working directory before renaming a file.

Ftp Updated 6 Oct 2019 1 min read

Method Name

rename

Method Signature

rename(fromname, toname)

Parameters

  • fromname - Current name of the file.
  • toname – The name to which the name of the file has to be changed.

Return value

On successful renaming returns an FTP message “250 File successfully renamed or moved”. In case of failure an error message like “550 Permission denied”

Overview

  • Renames a file in the FTP server to a given name as specified by the parameter toname.

Example

python
from ftplib import FTP

# Create an FTP instance

ftpInstance = FTP();

# Connect

resp = ftpInstance.connect(host="someexampleserver.com");

print(resp);

# Login

resp = ftpInstance.login(user="user@ someexampleserver.com ", passwd="gtb%4wectfd");

print(resp);

# Change working directory

resp = ftpInstance.cwd("/upload");

print(resp);

# Rename a file

resp = ftpInstance.rename("ToServer.txt", "NewFile.txt");

print(resp);

Output

Output
220 (vsFTPd 3.0.3)

230 Login successful.

250 Directory successfully changed.

226 Transfer complete.

250 File successfully renamed or moved.