DEV Community

hrishikesh1990
hrishikesh1990

Posted on • Originally published at flexiple.com

Python rename file: How to rename a file in Python

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.

Table of contents

  1. Rename file in Python
  2. Using os.rename() method to rename file
  3. Renaming only the Extension of the file in Python
  4. Closing thoughts

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)
Enter fullscreen mode Exit fullscreen mode

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.")
Enter fullscreen mode Exit fullscreen mode

Output:

The file has been renamed.
Enter fullscreen mode Exit fullscreen mode

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 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
for file_name in file:

# construct full file path
old_file_name = os.path.join(folder, file_name)

# Change the extension from txt to pdf
new_file_name = old_file_name.replace('.txt', '.pdf')
os.rename(old_file_name, new_file_name)
print('After rename:')
print(file)
Enter fullscreen mode Exit fullscreen mode

Output:

Before rename:
file.txt
After rename:
file.pdf
Enter fullscreen mode Exit fullscreen mode

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 name and other functions. One can learn more about other Python data types here.

Top comments (0)