import os # Specify the current and new file names current_file_name = "old_file.txt" new_file_name = "new_file.txt"
# Rename the file os.rename(current_file_name, new_file_name)
The original file should have now been renamed under the new name.
The os.rename()
method in Python is used to rename a file or directory. It belongs to the os
module, which provides a portable way of using operating system-dependent functionality.
Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.