Thanks to visit codestin.com
Credit goes to flexiple.com

Flexiple Logo
  1. Home
  2. Blogs
  3. Python
  4. Python rename file: How to rename a file in Python

Python rename file: How to rename a file in Python

Author image

Rajat Gupta

Software Developer

Published on Mon Apr 11 2022

There are many instances when you decide to name your file something but later regret it and want to rename the file. It is not as simple as renaming a folder in your computer system, but in Python, renaming a file is a very easy task. In this blog, we will see various methods to rename files.

Rename file in Python

In order to rename a file, the first thing we need is the path of the file. The path is the location of the file on the disk in a computer system. To be more particular, an Absolute path contains the complete directory list required to locate the file and a Relative path contains the current directory and then the file name.

Using os.rename() method to rename file

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. Python rename() file is a method used to rename a file or a directory in Python programming and can be declared by passing two arguments named src (Source) and dest (Destination).

Syntax:

os.rename(src, dest, *, src_dir, dest_dir)

Parameters:

src: A path-like object representing the file system path. This is the source file path that is to be renamed.
dest: Destination is the new name of the file or directory you want to change.
src_dir: Source file directory is an optional parameter telling where the file is stored.
dest_dir: The destination file directory is also an optional parameter telling where the renamed file should be saved on the disk.

Input:

# importing the os module
import os

# Source
src = 'filee.text'

# Destination
dest = 'file.txt'

# Renaming the file
os.rename(src, dest)
print("The file has been renamed.")

Output:

The file has been renamed.

This method does not have any return type. Keep in mind if the "dest" already exists then the FileExistsError will be thrown in Windows and in the case of UNIX, an OSError will be thrown.

Renaming only the Extension of the file in Python

Sometimes you might want to rename the extension of your file and this can be quickly done using the rename() method in Python. This can be done by selecting the file and then getting only the file name using the splitext() method of the os module. This method returns the root and extension separately. Once we get the root/base of the filename, we can add the new extension to it while renaming it using the rename() method.

Input:

import os

# Selecting the list
print('Before rename:')
file = 'file.txt'
print(file)

# Renaming the file
# Change the extension from txt to pdf
new_file_name = file.replace('.txt', '.pdf')
os.rename(file, new_file_name)
print('After rename:')
print(new_file_name)

Output:

Before rename:
file.txt
After rename:
file.pdf

Closing thoughts

Renaming a file in Python is as easy as naming a file. The os module in Python is used to rename a file and perform other functions.

Related Blogs

Browse Flexiple's talent pool

Explore our network of top tech talent. Find the perfect match for your dream team.