Prerequisite : OS module in Python
In Python3, rename() method is used to rename a file or directory. This method is a part of the os module and comes extremely handy.
Syntax for os.rename() :
os.rename(src, dst) : src is source address of file to be renamed and dst is destination with the new name.
Now say given n images in a folder having random names. For example, consider the image below:

Now the requirement is to rename them in ordered fashion like hostel1, hostel2, …and so on. Doing this manually would be a tedious task but this target can be achieved using the rename() and listdir() methods in os module.
The listdir method lists out all the content of a given directory.
Syntax for listdir() :
list = os.listdir(‘Src’) : Where Src is the source to be listed out.
The following code will do the job for us. It traverses through the lists of all the images in xyz folder, defines the destination (dst) and source (src) addresses and renames using rename module.
Below is the implementation :
# Pythono3 code to rename multiple # files in a directory or folder # importing os module import os # Function to rename multiple files def main(): for count, filename in enumerate(os.listdir("xyz")): dst ="Hostel" + str(count) + ".jpg" src ='xyz'+ filename dst ='xyz'+ dst # rename() function will # rename all the files os.rename(src, dst) # Driver Code if __name__ == '__main__': # Calling main() function main() |
Output :
The output of this code will look something like this –

Note : This code may not run in online IDE, since it use external image file directory.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Joining Excel Data from Multiple files using Python Pandas
- Rename all file names in your directory using Python
- Python | Write multiple files data to master file
- Python | How to use Multiple kv files in kivy
- Convert multiple JSON files to CSV Python
- Rename a folder of images using Tkinter
- Python | Pandas Dataframe.rename()
- Python | Pandas TimedeltaIndex.rename
- Python | Pandas Series.rename()
- Python | os.rename() method
- Python MongoDB- rename()
- MoviePy – Concatenating multiple Video Files
- MoviePy – Stacking multiple Video Files
- How to rename columns in Pandas DataFrame
- Rename specific column(s) in Pandas
- Downloading files from web using Python
- Check if directory contains files using python
- Uploading files on Google Drive using Python
- Compare two files using Hasing in Python
- Create temporary files and directories using Python-tempfile
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ChristopherGarland

