- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to rename multiple files recursively using Python?
You can use os.walk to recursively walk through the directory and subsequently use os.rename to rename the files.
import os def replace(folder_path, old, new): for path, subdirs, files in os.walk(folder_path): for name in files: if(old.lower() in name.lower()): file_path = os.path.join(path,name) new_name = os.path.join(path,name.lower().replace(old,new)) os.rename(file_path, new_name)
You can use this function as follows −
replace('my_folder', 'IMG', 'Image')
This will find all files recursively within the folder and its sub-folders and replace IMG with Image in each one of them. You can modify the function as it suits you for a better result suited to your need.
- Rename multiple files using Python
- Rename multiple files using Java
- How to rename multiple files in a directory in Python?
- How to touch all the files recursively using Python?
- How to rename directory using Python?
- How to use Glob() function to find files recursively in Python?
- List files recursively in C#
- How to rename a file using Python?
- How to merge multiple files into a new file using Python?
- How to spilt a binary file into multiple files using Python?
- How to zip a folder recursively using Python?
- How to create a directory recursively using Python?
- How to remove a directory recursively using Python?
- How to share common data among multiple Python files?
- How to convert PDF files to Excel files using Python?