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.
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()}")
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 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(): src=file dst="Dog"+str(i)+".jpg" os.rename(src,dst) i+=1
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(): 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+=1
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
- How to rename a file in Python?
- How to change the name of an image in Python?
- How to check if a file exists in Python?
- How to rename multiple file using Python?
- How to write to a file in Python?
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.
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google