How to rename directory using Python?


There are times when you want to organize your files or update the directory names to better reflect their contents. In such scenarios, the operation of renaming directories becomes helpful and it is a common task that we often encounter in Python file management. There is provision of several modules and methods in Python that makes renaming directories an easy task. In this article, we explore different ways how you can rename directories using Python code. You will learn this skill by practicing the code examples and explanations discussed in this article.

Making Use of the OS Module

The os module in Python has built-in functions for working with the operating system, involving operations such as renaming files and directories. We can utilize the os.rename() function from the os module to rename directories. Let us consider an example that showcases how to rename a directory using the os module:

Example

We start by defining a function called rename_directory that takes two parameters: old_name and new_name. These parameters represent the old name of the directory to be renamed and the new name that will replace the old name.

Within the function, we make use of the os.rename() function and pass the old_name and new_name as arguments. This function renames the directory by changing its name to the new name indicated.

import os
def rename_directory(old_name, new_name):

    os.rename(old_name, new_name)

# Example usage
rename_directory('old_directory', 'new_directory')

So when the above code is executed, the old_directory gets renamed as new_directory and there won't be old_directory anymore.

Making Use of the Shutil Module

The shutil module is yet another powerful module in Python that gives access to high-level file operations. It offers a useful method called shutil.move() that can be used to rename directories. Let us see an example that demonstrates how to rename a directory using the shutil module:

Example

Here, we define a function called rename_directory that has the same parameters as in the previous method: old_name and new_name.

Inside the function, we utilize the shutil.move() function and pass the old_name and new_name as arguments. This function moves the directory from the old name to the new name, effectively renaming it.

import shutil

def rename_directory(old_name, new_name):
    shutil.move(old_name, new_name)

# Example usage
rename_directory('old_directory', 'new_directory')

So when the above code is executed, the old_directory gets renamed as new_directory and there won't be old_directory anymore.

Making Use of the Pathlib Module

The pathlib module in Python allows us to use an object-oriented approach for working with file paths. It offers a powerful method called Path.rename() that can be used to rename directories. This is an example that demonstrates how to rename a directory using the pathlib module:

Example

  • We begin by importing the Path class from the pathlib module; this class represents a path to a file or directory in a system-independent way.

  • We next define a function called rename_directory that has the old_name and new_name parameters, similar to the previous methods.

  • Within the function, we create a Path object by passing the old_name to the Path() constructor.

  • At last we make use of the rename() method on the path object and pass the new_name as the argument. This method renames the directory to the new name specified.

from pathlib import Path

def rename_directory(old_name, new_name):
    path = Path(old_name)
    path.rename(new_name)

# Example usage
rename_directory('old_directory', 'new_directory')

So when the above code is executed, the old_directory gets renamed as new_directory and there won't be old_directory anymore.

Making Use of the OS Module with Absolute Paths

In some situations, you may have to work with absolute paths in place of relative paths when renaming directories. This is an example that shows how to rename a directory using the os module with absolute paths:

Example

  • At first, we define a function called rename_directory that takes the old_name and new_name parameters.

  • Inside the function, we utilize the os.path.abspath() function to get the absolute path of the old_name directory.

  • We then use the os.path.dirname() function to extract the parent directory from the absolute path.

  • Making use of the os.path.join() function, we create the absolute path of the new directory by concatenating the parent directory and the new_name.

  • Lastly, we use the os.rename() function to rename the directory by passing the absolute paths of the old and new directories as arguments.

import os

def rename_directory(old_name, new_name):
    abs_old_path = os.path.abspath(old_name)
    abs_new_path = os.path.join(os.path.dirname(abs_old_path), new_name)
    os.rename(abs_old_path, abs_new_path)

# Example usage
rename_directory('old_directory', 'new_directory')

So when the above code is executed, the old_directory gets renamed as new_directory and there won't be old_directory anymore.

Making Use of the Shutil Module

The shutil module in Python makes available a high-level interface for file and directory operations. It has, among others, a function called shutil.move() that can be used to rename directories. Here is an example that describes how to rename a directory using the shutil module:

Example

First, we import the shutil module; this module provides the necessary functions for file and directory operations.

We go on to define a function called rename_directory that takes the old_name and new_name parameters.

We use the shutil.move() function within the function and pass the old_name and new_name as arguments. This function moves the directory from the old name to the new name, effectively renaming it.

import shutil

def rename_directory(old_name, new_name):
    shutil.move(old_name, new_name)

# Example usage
rename_directory('old_directory', 'new_directory')

So when the above code is executed, the old_directory gets renamed as new_directory and there won't be old_directory anymore.

Making Use of the OS Module with os.replace()

The os module in Python has a rich set of tools for renaming directories. One such tool is the function os.replace(), which can be used to rename directories by replacing the old name with the new name. Let us consider an example that helps understand how to rename a directory using the os module and os.replace():

Example

We begin by importing the os module.

We define a function called rename_directory that has the old_name and new_name parameters.

We use the os.replace() function inside the function and pass the old_name and new_name as arguments. This function replaces the directory with the new name, in effect renaming it.

import os
def rename_directory(old_name, new_name):

    os.replace(old_name, new_name)

# Example usage
rename_directory('old_directory', 'new_directory')

So when the above code is executed, the old_directory gets renamed as new_directory and there won't be old_directory anymore.

We have by now seen that renaming directories is a basic file operation in Python, There are several tools and resources such as built-in modules and their functions and methods in Python that are used to rename directories. In this article, we have explored and discussed in detail ways to rename directories using the os and shutil modules. If you prefer the simplicity of the os.rename() function or the versatility of the shutil.move() function, you have the choice to rename directories effortlessly. We also explored two additional methods: using the pathlib module and working with absolute paths using the os module. Whether you prefer the object-oriented approach of pathlib or the absolute path manipulation of the os module, you have at your disposal the tools to rename directories with ease.

Updated on: 25-Jul-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements