Rename() Method Of FTP Class In Python

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:

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:

220 (vsFTPd 3.0.3)

230 Login successful.

250 Directory successfully changed.

226 Transfer complete.

250 File successfully renamed or moved.

 


Copyright 2022 © pythontic.com