In order to rename a file using Python, you can make use of the os.rename method from the os module.
os.rename(old_filename, new_filename)
Program:
'''
Rename a file in Python
Author : Code2care.org
Version : v 1.0
Date : 3 July 2023
'''
import os
def rename_file(old_filename, new_filename):
try:
os.rename(old_filename, new_filename)
print(f"{old_filename} renamed to {new_filename} successfully.")
except OSError as error:
print(f"Error occurred while renaming the file: {old_filename}", error)
old_file_name = 'data_draft.csv'
new_file_name = 'data_2023.csv'
rename_file(old_file_name, new_file_name)
Note: If the destination file already exists a FileExistsError is always raised.
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Python-Programs,
- Program 5: Find Sum of Two Integer Numbers - 1000+ Python Programs
- 34: Traverse a List in Reverse Order - 1000+ Python Programming
- 22: Send Yahoo! Email using smtplib - SMTP protocol client using Python Program
- 35: Python Program to find the System Hostname
- 27: Measure Elapsed Time for a Python Program Execution
- Program 7: Find Difference of Two Numbers - 1000+ Python Programs
- Program 12: Calculate Area and Circumference of Circle - 1000+ Python Programs
- Program 9: Divide Two Numbers - 1000+ Python Programs
- Program 2: Print your name using print() function - 1000+ Python Programs
- 25: How to rename a file using Python Program
- 17: Find Factorial of a Number - 1000+ Python Programs
- Program 6: Find Sum of Two Floating Numbers - 1000+ Python Programs
- 23: Python Programs to concatenate two Lists
- 36: Python Program Convert Hex String to Integer
- 20 - Python - Print Colors for Text in Terminal - 1000+ Python Programs
- Python Program: Use NumPy to generate a random number between 0 and 1
- 32: Python Program to Find Square Root of a Number
- Program 8: Multiply Two Numbers - 1000+ Python Programs
- Program 11: Calculate Percentage - 1000+ Python Programs
- 18: Get Sub List By Slicing a Python List - 1000+ Python Programs
- 28: Program to Lowercase a String in Python
- Program 1: Print Hello World! - 1000+ Python Programs
- 21: Program to Delete File or Folder in Python
- 29: Program to convert Python dict to dataframe
- 33: Python Program to find the current time in India (IST)
More Posts:
- Reading .xls and .xlsx Excel file using Apache POI Java Library - Java
- How to change file or directory owner in Linux - Linux
- Multiline EditText in Android Example - Android
- Show Desktop Keyboard Shortcut on Mac - MacOS
- Configure AWS Access ID and Secret Keys using CLI on Mac - AWS
- How to run a Command in Bash Script - Bash
- How to get Mobile Screen Resolution Width and Height programatically - Android
- [macOS] How to search or view previous terminal command history - MacOS