Python os.renames() Method

The os.renames() method in Python is used to rename directories or files recursively, moving them if necessary. It creates any intermediate directories needed to make the new pathname good.

After moving the files or directories, it will remove empty directories along the old path.

This method throws an OSError for issues like insufficient permissions.


os.renames(old, new)


Name Description
old (string) It is a path to the file or directory you want to rename.
new (string) It is a new name or path for the file or directory.

Return value

This method does not return any value.

Example 1: Basic file renaming

In the “dir1” directory, we have a file called “old_file.txt” which we will rename to a “new_old.txt” file using this method.

Screenshot of dir1 current status

import os

# Renaming a file
os.renames('dir1/old_file.txt', 'dir1/new_file.txt')

print("File renamed successfully")


File renamed successfully

Output of os.renames() function

Example 2: Renaming and moving a file to a new directory

Currently, inside the “dir1” directory, there is only one file called new_file.txt and no other directories. Here is the screenshot of it:

Screenshot of current status

Let’s create a new directory and rename and move the “new_file.txt” in that directory.

import os

# Renaming a file
os.renames('dir1/new_file.txt', 'dir1/dir2/latest_file.txt')

print("File renamed and moved successfully")


File renamed and moved successfully

Here is the current status of the “dir1” directory.

Created a new directory inside dir1

Here is the current status of the “dir2” directory.

Output of renaming and moving the file to newly created directory

Example 3: Renaming a directory

Here is the current status of “dir2” before renaming it:

Screenshot of before renaming a directory

Let’s rename the directory “dir2”, which is inside the “dir1” directory.

import os

# Renaming a directory
os.renames('dir1/dir2', 'dir1/new_dir')

print("Directory Successfully renamed")


Directory Successfully renamed

After renaming a directory:

Output of Renaming a directory using os.renames() method

Example 4: Error Handling

If you try to rename a non-existent file, it will throw the FileNotFoundError.

import os

# Renaming a file
os.renames('dir1/nonexistent_file.txt', 'dir1/new_file.txt')

print("File Successfully renamed")


FileNotFoundError: [Errno 2] No such file or directory: 'dir1/nonexistent_file.txt' -> 'dir1/new_file.txt'

Since there is no nonexistent_file.txt, it couldn’t rename it, and hence FileNotFoundError occurs.

Here is how you can handle it using the try-except block:

import os

  # Renaming a file
  os.renames('dir1/nonexistent_file.txt', 'dir1/new_file.txt')
except FileNotFoundError as error:
  print("The file does not exist")


The file does not exist

Difference between os.rename() and os.renames()

Parameters os.rename() os.renames()
Basic Functionality The os.rename() method is used for straightforward renaming or moving without the need to handle the creation of new directories or cleanup of old paths. The os.renames() method is more comprehensive and designed for complex operations involving creating new directory structures or cleaning up after moving files and directories.
Directory Creation It does not create intermediate directories if they do not exist in the new path. If the new path requires creating new intermediate directories, os.renames() will handle this automatically.
Recursive Handling It does not handle the removal of empty directories left behind after moving files or directories. After moving the files or directories, it will remove empty directories along the old path.

That’s all!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.