How Python Rename File – Single & Multiple Files With Example

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

In our last Python tutorial, we studied How Python Copy a File. Today, we will see how Python rename file – Single and multiple files.

For renaming files in python, we will use the method rename() from the module os.

Let’s begin How Python Rename File.

How Python Rename File - Single & Multiple Files With Example

How Python Rename File – Single & Multiple Files With Example

What is os.rename() in Python?

OS.rename() is used to python rename file. Let’s first take a look at the rename() method from the os module.

Like the name suggests, it lets us rename files and directories

A Syntax of os.rename()

We have the following syntax for the rename() method:

os.rename(src, dst)

Here, src is the source file or directory. dst is the destination file or directory. It doesn’t return any value. Let’s take an example.

os.rename() Example

In the following example, we rename the folder ‘NewFolder’ to ‘Photos’.

>>> import os,sys
>>> os.chdir('C:\\Users\\lifei\\Desktop')
>>> print(f"We have: {os.listdir()}")


We have
[‘0.jpg’, ‘1.txt’, ‘Documents’, ‘NewFolder’, ‘Today.txt’]
>>> os.rename('NewFolder',’Photos’) #This does the renaming
>>> os.listdir()


[‘0.jpg’, ‘1.txt’, ‘Documents’, ‘Photos’, ‘Today.txt’]

As you can see, this has renamed the directory ‘NewFolder’ to ‘Photos’.

At the time of writing this article, we tried to rename the directory to the name ‘Documents’.

This raised the following error:

>>> os.rename('NewFolder','Documents')


Traceback (most recent call last):File “<pyshell#7>”, line 1, in <module>


FileExistsError: [WinError 183] Cannot create a file when that file already exists: ‘NewFolder’ -> ‘Documents’

How Python Rename Multiple Files?

Suppose in this folder ‘Photos’, we have the following photos of dogs.

Here, we can manually rename each of these files, but what if we had over a hundred photos? Or maybe thousands? It would be too much to do.

Instead, we make Python do it. Watch how.

>>> import os
>>> os.chdir('C:\\Users\\lifei\\Desktop\\Photos')
>>> i=1
>>> for file in os.listdir():

Python Rename File (Single File)

Sometimes, we may want to rename just one file from tens or hundreds. We can search for it and then use Python to rename it.

In the following code, we rename ‘Dog7.jpg’ to ‘SeventhDog.jpg’.

>>> for file in os.listdir():
    if src=='Dog7.jpg':
           dst="Seventh Dog.jpg"

An Example- Incrementing Each Picture Number

In this example, we increment the picture number for each picture in this folder.

For this example, we have renamed ‘Seventh Dog.jpg’ to ‘Dog7.jpg’.

>>> print("How many pictures?")


How many pictures?
>>> i=int(input())


>>> count=1
>>> while(count<=10): 
         for file in os.listdir(): 
                 if src=='Dog'+str(i)+'.jpg': 

This code does the following:

What we do here is that we ask the user how many pictures she has. We set a variable count to 1. We keep processing till count becomes equal to 10.

For each time we are in the while loop, we check the filenames one by one. We compare the file names; the first question is- Is the filename ‘Dog10.jpg’?

For the file it is, it renames it by incrementing the number. Then, we decrement i by 1 and increment count by 1.

Hence, once count has reached 11, we have fulfilled our purpose by now.

So, this was all about How Python Rename File tutorial. Hope you like our explanation.

Python Interview Questions on Rename File

  1. How to rename a file in Python?
  2. How to change the name of an image in Python?
  3. How to check if a file exists in Python?
  4. How to rename multiple file using Python?
  5. How to write to a file in Python?


Hence, we learned how Python rename file(single and multiple files) with os.rename module.

In addition, we discuss an example to understand this process better. Got more ideas? Drop them in the comments.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google


Leave a Reply

Your email address will not be published. Required fields are marked *