Copy a File to Another Location using Python
In Python, you can copy a file to another location using the shutil module, which provides a function called shutil.copy() or shutil.copy2(). These functions allow you to duplicate a file from one directory to another efficiently while preserving the file’s content.
Examples to Copy a File to Another Location
1. Copy a File to Another Directory Using shutil.copy()
In this example, we will copy a file named source.txt from the current directory to a folder named backup using the shutil.copy() function.
main.py
import shutil
# Define source and destination file paths
source_file = "source.txt"
destination_folder = "backup/"
destination_path = destination_folder + "source_copy.txt"
# Copy the file
shutil.copy(source_file, destination_path)
print("File copied successfully!")
Explanation:
- We import the
shutilmodule, which provides file copy functionalities. - Define the
source_fileas"source.txt", which is the file we want to copy. - Set
destination_folderas"backup/", where we want to copy the file. - Define the complete destination path, including the filename, using
destination_path. - Use
shutil.copy(source_file, destination_path)to copy the file. - Print a success message confirming that the file was copied.
Output:


2. Copy a File While Preserving Metadata Using shutil.copy2()
In this example, we will use shutil.copy2(), which not only copies the file but also retains metadata such as timestamps.
main.py
import shutil
# Define source and destination file paths
source_file = "data.txt"
destination_file = "backup/data_copy.txt"
# Copy the file along with metadata
shutil.copy2(source_file, destination_file)
print("File copied successfully with metadata!")
Explanation:
- Import the
shutilmodule. - Specify
source_fileas"data.txt", the file to be copied. - Set
destination_fileas"backup/data_copy.txt", the target location. - Use
shutil.copy2(source_file, destination_file)to copy the file along with metadata. - Print a confirmation message indicating that the file was copied with metadata.
Output:
File copied successfully with metadata!
3. Copy a File and Rename It in the New Location
In this example, we will copy a file to a new directory and rename it at the same time.
main.py
import shutil
# Define source and new destination file paths
source_file = "report.pdf"
destination_file = "documents/report_backup.pdf"
# Copy and rename the file
shutil.copy(source_file, destination_file)
print("File copied and renamed successfully!")
Explanation:
- Import the
shutilmodule. - Define
source_fileas"report.pdf", the file to be copied. - Set
destination_fileas"documents/report_backup.pdf", renaming the copied file. - Use
shutil.copy(source_file, destination_file)to copy and rename the file. - Print a success message confirming the copy operation.
Output:
File copied and renamed successfully!
4. Copy a File Using os Module
The os module provides another way to copy a file using os.system() with shell commands.
main.py
import os
# Define source and destination paths
source_file = "image.jpg"
destination_file = "backup/image_copy.jpg"
# Copy using shell command
os.system(f"cp {source_file} {destination_file}")
print("File copied using os module!")
Explanation:
- Import the
osmodule. - Define
source_fileas"image.jpg", the file to copy. - Set
destination_fileas"backup/image_copy.jpg", the new location. - Use
os.system()with the shell commandcpto copy the file. - Print a message confirming that the file was copied.
Output:
File copied using os module!
Conclusion
Python provides multiple ways to copy a file to another location:
- Using
shutil.copy()for a basic file copy. - Using
shutil.copy2()to retain metadata. - Using
os.system()for shell command-based copying.
