

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Rename multiple files using Python
rename() method is used to rename a file or directory in Python3. The rename() method is a part of the os module.
Syntax for os.rename()
os.rename(src, dst)
The first argument is src which is source address of file to be renamed and second argument dstwhich is the destination with the new name.
Let's take any directory which has one image folder. Here we have this image folder.
Input

Example code
import os # Function to rename multiple files def main(): i = 0 path="C:/Users/TP/Desktop/sample/Travel/west bengal/bishnupur/" for filename in os.listdir(path): my_dest ="soul" + str(i) + ".jpg" my_source =path + filename my_dest =path + my_dest # rename() function will # rename all the files os.rename(my_source, my_dest) i += 1 # Driver Code if __name__ == '__main__': # Calling main() function main()
Output

- Related Questions & Answers
- Rename multiple files using Java
- How to rename multiple files recursively using Python?
- How to rename multiple files in a directory in Python?
- Copy, Rename and Delete Files in Perl
- Multiple .java files
- How to rename directory using Python?
- How to spilt a binary file into multiple files using Python?
- How to merge multiple files into a new file using Python?
- How to rename a file using Python?
- Python - Rename column names by index in a Pandas DataFrame without using rename()
- Python - Write multiple files data to master file
- Rename a table in MySQL using RENAME TABLE command
- Using G++ to compile multiple .cpp and .h files
- How to share common data among multiple Python files?
- How we can split Python class into multiple files?
Advertisements