Change language

Python | os.rename () method

|

os.rename() in Python is used to rename a file or directory. 
This method renames the source file / directory to the specified target file / directory.

Syntax: os.rename (source, destination, *, src_dir_fd = None , dst_dir_fd = None)

Parameters:
source: A path-like object representing the file system path. This is the source file path which is to renamed.
destination: A path-like object representing the file system path.
src_dir_fd (optional) : A file descriptor referring to a directory.
dst_dir_fd (optional): A file descriptor referring to a directory.

Return Type: This method does not return any value.

Code # 1: Using the os. rename ()

# Python program to explain the os.rename () method

 
# import of the os module

import os

  

 
# Path to is source file

source = ’Python.Engineering / file.txt’

 
# path to destination file

dest = ’GeekforGeeks / newfile.txt’

 

 
# Now rename the source path
# to the destination path
# using the os.rename method ()
os.rename (source, dest)

print ( "Source path renamed to destination path successfully." )

Exit:

 Source path renamed to destination path successfully. 

Code # 2: Handling Potential Errors

# Python program to explain the os.rename () method

 
# import of the os module

import os

 

 
# Source file path

source = ’. / Python.Engineering / file.txt’

 
# path to destination file

dest = ’. / Python.Engineering / dir’

  

  
# try renaming the source path
# to the destination path
# using the os.rename () method

 

try :

os.rename (source, dest)

print ( "Source path renamed to destination path successfully." )

 
# If the source is a file
# but the destination is a directory

except IsADirectoryError:

print ( " Source is a file but destination is a directory. " )

  
# If source is a directory
# but the destination is a file

except NotADirectoryError:

print ( "Source is a directory but destination is a file." )

 
# For error-related permissions

except PermissionError:

print ( "Operation not permi tted. " )

  
# For other errors

except OSError as error:

print (error)

Exit:

 Source is a file but destination is a directory. 

Link: https://docs.python.org/3/library/os.html#os.rename

Python | os.rename () method Python functions: Questions

Python | os.rename () method rename: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method