Create a Duplicate File of an Existing File Using Python



In Python, duplicates of an existing file are created as backups to manipulate data and to preserve original version. To create a duplicate file of an existing file using Python we can use the shutil module or pathlib module, which we will discuss in detail with examples.

Here are the various approaches for this:

Creating Duplicate File of an Existing File Using shutil Module

The shutil module is used for copying and removing of files, as it offers large number of operations on files and collection of files. Following are some of the examples with the functions used to create duplicate file of an existing file using:

Example : Using shutil.copy()


The shutil.copy() function in shutil module copies contents in a file src (source) from one location to another dst (destination) file. This function creates a duplicate file by copying the contents and metadata from source to destination. This function accepts the source and destination file paths as parameters.

In the example below, we use the shutil module to copy the file module.py to a new file called module_copy.py, assuming that module.py file exists. It it doesn't exist, an error will be raised when the copy() function is used-

import shutil

# Copy the file along with metadata
shutil.copy('module.py', 'module_copy.py')

# Output confirmation
print("Duplicate file created for the existing file.")

After the copying is complete, the code will return the following output -

Duplicate file created for the existing file.

Example : Using the shutil.copyfile()


The shutil.copyfile() is similar to the shutil.copy() function which we have discussed in the above example, except that it does not use file objects and copy metadata and permissions

The code is similar to the above example, you just have to replace the shutil.copy() function with the shutil.copyfile() as given below -

import shutil

# Copy the file content only (without metadata)
shutil.copyfile('module.py', 'module_copy.py')

# Output confirmation
print("Duplicate file created for the existing file.")

After the copying is complete, the code will return the following output -

Duplicate file created for the existing file.

Creating Duplicate File of an Existing File Using open() Function

Here, we will use the manual method to create a duplicate file of an existing file by using the open() (built in function which opens a object and returns a file object) function with the 'r' mode to get the content from the source file (module.py) file and 'w' mode to write the same content into the duplicate file ( module_copy.py) .

Example

In the example below, we will use the open() function to create a duplicate file for an existing file -

# Read the original file
with open('module.py', 'r') as src:
    content = src.read()

# Write to a new duplicate file
with open('module_copy3.py', 'w') as dst:
    dst.write(content)

print("Duplicate file created for the existing file.")

After the copy is complete, the code will return the following output -

Duplicate file created for the existing file.

Creating Duplicate File of an Existing File Using pathlib

The pathlib module (allows to interact with files and directories using a n object-oriented approach) is usedto read the entire content in the source file (module.py) as string. For this, we use the read.text() function in pathlib module reads the content in a files and returns it in the form of string , and then write this string content to duplicate file ( module_copy.py) using write.text()function.

Example

In the example below, we will use pathlib module to create a duplicate file for an existing file -

from pathlib import Path

# Copy contents from original file to duplicate
Path('module_copy.py').write_text(Path('module.py').read_text())

print("Duplicate file created for the existing file.")

After the copy is complete, the code will return the following output -

Duplicate file created for the existing file.
Updated on: 2025-06-05T14:05:24+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started