
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
Example
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.
- Related Articles
- 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 use Glob() function to find files recursively in Python?
- How to rename directory 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 rename a file using Python?
- How to create a directory recursively using Python?
- How to remove a directory recursively using Python?
- How to zip a folder recursively using Python?
- List files recursively in C#
- How to share common data among multiple Python files?
- Python - How to rename multiple column headers in a Pandas DataFrame with Dictionary?

Advertisements