How Python Rename File – Single & Multiple Files With Example
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
Renaming files is a common task while organizing data, managing projects, or automating workflows.
We will also explore how to handle multiple files, apply patterns while renaming, and avoid common errors like missing files or incorrect paths.
For renaming files in Python, we will use the method rename() from the module os.
Let’s begin with how to rename a file in Python.
What is os.rename() in Python?
A messy naming scheme can break code. Python fixes it with os.rename(“old.txt”, “new.txt”). One call swaps names without touching content, making “rename files in Python” a common search for developers tidying folders.
As the name suggests, it lets us rename files and directories
Syntax of os.rename() in Python
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 Python
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()}")Output
[‘0.jpg’, ‘1.txt’, ‘Documents’, ‘NewFolder’, ‘Today.txt’]
>>> os.rename('NewFolder',’Photos’) #This does the renaming
>>> os.listdir()Output
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')Output
Traceback (most recent call last):File “<pyshell#7>”, line 1, in <module>
os.rename(‘NewFolder’,’Documents’)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: ‘NewFolder’ -> ‘Documents’
How to rename multiple files in Python?
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():
src=file
dst="Dog"+str(i)+".jpg"
os.rename(src,dst)
i+=1Rename File (Single File) in Python
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():
src=file
if src=='Dog7.jpg':
dst="Seventh Dog.jpg"
os.rename(src,dst)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?")Output
>>> i=int(input())
Output
>>> count=1
>>> while(count<=10):
for file in os.listdir():
src=file
if src=='Dog'+str(i)+'.jpg':
dst='Dog'+str(i+1)+'.jpg'
os.rename(src,dst)
i-=1
count+=1This 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 renames it by incrementing the number. Then, we decrement i by 1 and increment count by 1.
Hence, once the count has reached 11, we have fulfilled our purpose by now.
So, this was all about the ” How Python Renames Files 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?
Conclusion
Hence, we learned how to rename files (single and multiple files) with the os.rename module in Python.
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

