How Python Rename File – Single & Multiple Files With Example

Free Python course with 25 projects (coupon code: DATAFLAIR_PYTHON) Start Now

1. Objective

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

2. os.rename()

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.
Let’s Discuss a List of 63 Python os Modules in 2018

a. 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.

b. 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’
Let’s Discuss Errors and Exceptions in Python Programming

3. 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():

Read about Exception Handling in Python 

4. 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"

5. 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.

6. Conclusion

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.
Related Topic- Python Zipfile
For reference

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.