Python Rename File: A Complete Guide

Posted in /  

Python Rename File: A Complete Guide
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    Python provides the os module to perform the operating system-dependent functionalities. There is a method in the os module rename() that can rename any file and directory. The rename() method accepts two arguments the source file or directory name and the destination file and directory name. The source name represents the current name of the file and the destination name represents the new name.

    While working with files in Python, we encounter many cases where we need to rename an existing file. For instance, we have a file by name data.txt and after appending or writing in the file if we wish to change its name to updated_data.txt , there we can use the os.rename() method. The os.rename() method will allow us to rename an existing file using Python only. This Python guide will discuss the os.rename() method with some examples. First, we will see how to rename a single file and directory using Python. Then also walk through an example to change multiple files' names.

    Python Rename File

    To rename a file using Python, we can use the Python os.rename() method. This method accepts the exiting file path along with the file name and the new name of the file and renames the file with the new name.

    Syntax

    import os
    os.rename(source, destination)

    The rename accepts two argument values(string).

    source : The source argument can be a file name or file path along with the file name that we want to rename (e.g. "C:\Users\guest\Desktop\data.txt").

    destination : The destination argument is the new name of the existing file. It can also be a full path to the new name.

    Note: If we pass an already existing file name as a new name to the destination argument, we will receive the error FileExistsError

    Example Rename a File in Python

    # import module
    import os
    
    # file path along with file name
    file = r"C:\lenovo\lenovo\Desktop\code\data.txt"
    
    
    # new name of the file
    new_name = "C:\Users\lenovo\Desktop\code\data_updated.txt"
    
    os.rename(file, new_name)
    print("The name has been changed")
    

    Output

    The name has been changed
    
    

    We can also change the directory name using the os.rename() method.

    # import module
    import os
    
    # file path along with file name
    directory = r"C:\Users\lenovo\Desktop\code\Game"
    
    
    # new name of the directory along with path
    new_name = "C:\Users\lenovo\Desktop\code\game folder"
    
    os.rename(directory, new_name)
    print("The name has been changed")
    

    Python Rename Multiple files

    Let's say we have two files main.cpp and main.py in our C:\Users\lenovo\Desktop\code\cubic directory, and we wish to rename them with main1.cpp and main1.py . In that case, we can use the rename() method to rename both of the files. rename() method can only rename one file or directory at a time, so in order to change multiple files, we need to loop through the directory and check for the specific file. To check all the files present in a folder, we can use the os.listdir() method.

    Example change multiple files

    # import module
    import os
    
    # files to rename
    files = ['main.py', 'main.cpp']
    
    # loop through directory files
    for file_name in os.listdir(r"C:\Users\lenovo\Desktop\code\cubic"):
    
    
        if file_name in files:
            # it will replace .py or ,cpp name to 1.py and 1.cpp
            file_new_name =  file_name.replace('.', '1.')
    
            os.rename(rf'C:\Users\lenovo\Desktop\code\cubic\{file_name}',rf'C:\Users\lenovo\Desktop\code\cubic\{file_new_name}')
    
    print('file names has been changed')
    

    Output

    files names has been changed

    Wrapping Up!

    Renaming a file in Python is very easy. All we need is the os.rename() method. When it comes to changing multiple files' names there, it could be tricky. There, you need to loop through the complete directory and check for the file names you want to change. The path of the file, along with the file name, is necessary if your Python script is located in a different directory. If your Python file and the file you want to rename are in the same directory, then you do not need to specify the full path to the file.

    People are also reading:

    Leave a Comment on this Post

    0 Comments