In this Python tutorial, I have explained, how to copy file and rename in Python.
Copy File and Rename in Python
Python provides various ways to copy and rename files:
- Using the
shutil
library for copying files. - Using the
os
library for renaming and handling file paths.
Let’s get started!
Using the shutil Library to Copy Files
The shutil
(shell utility) module in Python provides functions to operate on files and collections of files. It comes in handy when you want to copy files.
Here’s how you can use the shutil
library to copy a file:
Example 1: Basic File Copy
import shutil
# Specify the source file and the destination (including the new file name)
source_file = 'path/to/your/source/file.txt'
destination_file = 'path/to/your/destination/new_file.txt'
# Copy the file
shutil.copy2(source_file, destination_file)
In this example, shutil.copy2
is used to copy the file from the source to the destination. It also preserves the file’s metadata.
Using the os Library to Rename Files
While shutil
can be used for copying, the os
module is useful for renaming files, and also for handling file paths.
Here’s how you can use the os
library to rename a file:
Example 2: Basic File Rename
import os
# Specify the current file name and the new file name
current_file = 'path/to/your/current/file.txt'
new_file = 'path/to/your/new/file.txt'
# Rename the file
os.rename(current_file, new_file)
Here is the complete code:
import shutil
import os
# Specify the source file, the destination for the copy, and the new name
source_file = 'path/to/your/source/file.txt'
destination_directory = 'path/to/your/destination/'
new_file_name = 'new_file.txt'
# Copy the file
shutil.copy2(source_file, destination_directory)
# Get the base name of the source file
base_name = os.path.basename(source_file)
# Construct the paths to the copied file and the new file name
copied_file = os.path.join(destination_directory, base_name)
new_file = os.path.join(destination_directory, new_file_name)
# Rename the file
os.rename(copied_file, new_file)
Python copy file and rename
Here, we can see how to copy file and rename in Python.
- In this example, I have imported Python modules called shutil and os.
- Firstly, we have to copy the file from source to destination and then rename the copied file.
- src = r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\name.txt’ is the source path. name.txt is the file name that I have created with the .txt extension.
- dst = r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder\name.txt’ is the destination path. The name.txt is the file name that I have created with the .txt extension.
- os.rename is used to rename the folder name. To rename the file, I have used os.rename(r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\name.txt’,r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder\details.txt’)
- shutil.copyfile(src, dst) is used to copy the file from source to destination.
- The name.txt file name is now renamed by the name called details.txt.
Example:
import shutil
import os
src = r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\name.txt'
dst = r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder\name.txt'
shutil.copyfile(src, dst)
os.rename(r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\name.txt',r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder\details.txt' )
The above code, we can use to copy file and rename in Python.
You may like the following Python tutorials:
- Write String to a .csv File in Python
- Read the file as a string in Python [5 Methods]
- Get File Extension from File Name in Python
- Read a file line by line in Python
In this Python tutorial, I have explained, how to copy and rename file in Python.
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.