
- 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 a file using Python?
You can use os module's rename method. For example, you want to rename a file from a.txt to b.txt with these files present in your current directory,
>>> import os >>> os.rename('a.txt', 'b.txt')
You can also use the shutil (or shell utilities) module. Calling shutil.move(source, destination) will move the file or folder at the path source to the path destination and will return a string of the absolute path of the new location.
For example
>>> import shutil >>> shutil.move('a.txt', 'b.txt')
If you have the files in different directories, then specify the absolute path for both methods.
- Related Articles
- How to rename directory using Python?
- C# Program to rename a file
- Golang program to rename a file
- How to rename multiple files recursively using Python?
- C program to change the file name using rename() function
- Java Program to rename a file or directory
- Rename multiple files using Python
- How to delete a file using Python?
- How to remove a file using Python?
- How to find a file using Python?
- Python - Rename column names by index in a Pandas DataFrame without using rename()
- How to create a tar file using Python?
- How to create a zip file using Python?
- What are the steps to rename a file in Git?
- Golang Program to rename a specified file by another name

Advertisements