Python | os.renames() method

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

os.renames() method is a recursive directory or file renaming function. It works like os.rename() method except creation of any intermediate directories needed then it is attempted first. After the renaming is done then directories corresponding to rightmost path segments of the old name will be pruned away 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() method to rename a file

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.renames() method 
       
# importing os module 
import os 
   
# path 
path = 'C:/Users/Rajnish/Desktop/GeeksforGeeks'
  
# Changing directory 
os.chdir(path)
  
# Printing current directory
print ("Current directory is: ", os.getcwd())
  
# List files and directories 
# in 'C:/Users/Rajnish/Desktop/GeeksforGeeks'
print("Before renaming file:"
print(os.listdir(os.getcwd())) 
   
# Rename the file
# Using os.renames() method
os.renames('testfile.txt', 'new_name.txt'
   
# List files and directories 
# in 'C:/Users/Rajnish/Desktop/GeeksforGeeks'
print("After renaming file:"
print(os.listdir(os.getcwd())) 

chevron_right


Output:

Current directory is: C:\Users\Rajnish\Desktop\GeeksforGeeks
Before renaming file:
['testfile.txt']
After renaming file:
['new_name.txt']

Example #2 :
Using os.renames() method to rename a file and adding it into a new directory that do not exist

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.renames() method 
       
# importing os module 
import os 
   
# path 
path = 'C:/Users/Rajnish/Desktop/GeeksforGeeks'
# Changing directory 
os.chdir(path)
  
# Printing current directory
print ("Current directory is: " os.getcwd())
  
# List files and directories 
# in 'C:/Users/Rajnish/Desktop/GeeksforGeeks'
print("Before renaming file:"
print(os.listdir(os.getcwd())) 
   
# Rename the file and
# adding the file in new
# directory name 'newdir'
# Using os.renames() method
os.renames('testfile.txt', 'newdir / new_name.txt'
   
# List files and directories 
# in 'C:/Users/Rajnish/Desktop/GeeksforGeeks'
print("After renaming file:"
print(os.listdir(os.getcwd())) 

chevron_right


Output:

Current directory is: C:\Users\Rajnish\Desktop\GeeksforGeeks
Before renaming file:
['newdir', 'testfile.txt']
After renaming file:
['newdir']


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.