Copy And Replace Files in Python
Last Updated :
05 Feb, 2024
In Python, copying and replacing files is a common task facilitated by modules like `shutil` and `os`. This process involves copying a source file to a destination location while potentially replacing any existing file with the same name. Leveraging functions like `shutil.copy2` and `os.remove`, this operation is crucial for updating or maintaining file versions. In this article, we will see how to copy and replace files in Python.
Copy And Replace Files In Python
Below are some of the ways by which we can copy and replace files in Python:
Python Copy And Replace Files Using shutil.copy2
and os.remove() Function
In this example, the `copy_and_replace` function is defined to copy a source file to a destination location, replacing any existing file at the destination. The paths of the source and destination files are specified, and the function is then called to execute the copy and replace operation.
Python3
import shutil
import os
def copy_and_replace(source_path, destination_path):
if os.path.exists(destination_path):
os.remove(destination_path)
shutil.copy2(source_path, destination_path)
# Source and destination file paths
source_file = 'path/to/source_file.txt'
destination_file = 'path/to/destination_file.txt'
# Copy and replace
copy_and_replace(source_file, destination_file)
Output:

Copy And Replace Files In Python Using shutil.move()
In this example, the `copy_and_replace` function takes a source file path and a destination directory. It extracts the filename from the source path, constructs the destination path within the specified directory, removes any existing file with the same name, then moves the source file to the destination, effectively copying and replacing it. The example usage copies the 'main.py' file to the 'dummy' directory, providing a print confirmation upon successful completion.
Python3
import shutil
import os
def copy_and_replace(source_file, destination_directory):
filename = os.path.basename(source_file)
destination_path = os.path.join(destination_directory, filename)
if os.path.exists(destination_path):
os.remove(destination_path)
shutil.move(source_file, destination_path)
print(f"File '{filename}' copied and replaced successfully.")
# Example usage:
source_file_path = 'main.py'
destination_directory = 'dummy'
copy_and_replace(source_file_path, destination_directory)
Output:

Similar Reads
Copy Files and Rename in Python Copying and renaming files is a common task in programming, and Python provides several ways to accomplish this efficiently. In this article, we will explore some commonly used methods for copying files and renaming them: using the shutil module's copy() and pathlib module's Path class. Each method
2 min read
Python - os.replace() method Prerequisite: OS module in Python. os.replace() method in Python is used to rename the file or directory. If destination is a directory, OSError will be raised. If the destination exists and is a file, it will be replaced without error if the action performing user has permission. This method may fa
2 min read
How to copy file in Python3? Prerequisites: Shutil When we require a backup of data, we usually make a copy of that file. Python supports file handling and allows users to handle files i.e., to read and write files, along with many other file handling options, to operate on files. Here we will learn how to copy a file using Pyt
2 min read
Datetime.replace() Function in Python The datetime.replace() method in Python allows you to modify specific parts of a datetime or date object without changing the original object. Instead, it returns a new modified object with the updated values.For example, suppose we booked a flight for March 15, 2025, at 10:00 AM, but it got resched
3 min read
How to search and replace text in a file in Python ? In this article, weâll explore four effective methods to search and replace text within a file using Python. These methods range from basic built-in functions to powerful modules like re and fileinput.Method 1: Using Basic File Handling (open() and replace())Let see how we can search and replace tex
3 min read