Change language

Python | os.renames () method

|

os.renames () — it is a recursive directory or file renaming function. It works like os.rename () except it creates any intermediate directories that need to be done and then tries first. After renaming, directories corresponding to the rightmost segments of the old name path will be removed using os.removedirs () .

Syntax: os. renames (old, new)

Parameters:
old: This is the old name of the file or directory to be renamed.
new: This is the new name of the file or directory. It can include a file to a
directory or a whole tree of directories that do not exist.
Note: It can also accept a path-like object for old and new.

Return Value: This method does not returns any value.

Example # 1: Using os.renames () to rename a file

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

  
# import of the os module

import os 

 
# path

path = ’C: / Users / Rajnish / Desktop / Geeksf orGeeks’

 
# Change directory
os.chdir (path)

 
# Printing the current directory

print ( "Current directory is: " , os.getcwd ())

  
# List of files and directories
# in & # 39; C: / Users / Rajnish / Desktop / Python.Engineering & # 39;

print ( "Before renaming file:"

print (os.listdir ( os.getcwd ())) 

 
# Rename file # Using the os.renames () method

os.renames ( ’ testfile.txt’ , ’new_name.txt’

 
# List of files and directories
# in & # 39; C: / Users / Rajnish / Desktop / Python.Engineering & # 39;

print ( "After renaming file:"

print (os.listdir (os.getcwd ())) 

Exit :

 Current directory is: C: UsersRajnishDesktopPython.Engineering Before renaming file: [’testfile.txt’] After renaming file: [’new_name.txt’] 

Example # 2:
Using os.renames () to rename the file and add it to a new directory that does not exist

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

 
# import of the os module

import os 

  
# path

path = ’C: / Users / Rajnish / Desktop / GeeksforGeeks’

# Change directory
os.chdir (path)

 
# Print current directory

print ( "Current directory is: " os.getcwd ())

  
# List of files and directories
# in & # 39; C: / Users / Rajnish / Desktop / Python.Engineering & # 39;

print ( "Before renaming file:"

print (os.listdir (os. getcwd ())) 

 
# Rename the file and
# adding a file to a new one
# directory name & # 39; newdir & # 39;
# Using the os.renames () method

os.renames ( ’ testfile.txt’ , ’newdir / new_name.txt’

  
# List of files and directories
# in & # 39; C: / Users / Rajnish / Desktop / Python.Engineering & # 39;

print ( "After renaming file:"

print (os.listdir (os.getcwd ())) 

Exit:

 Current directory is: C: UsersRajnishDesktopPython.Engineering Before renaming file: [’newdir’ , ’testfile.txt’] After renaming file: [’ newdir’] 

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

Python | os.renames () 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