How to 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 Using shutil Module

The shutil module is used for copying and removing files, as it offers a large number of operations on files and collections of files. Following are examples of functions used to create duplicate files ?

Using shutil.copy()

The shutil.copy() function copies contents and metadata from a source file to a destination file. This function accepts the source and destination file paths as parameters.

import shutil

# First create a sample file to work with
with open('sample.txt', 'w') as f:
    f.write("This is a sample file for demonstration.\nIt contains multiple lines.")

# Copy the file along with metadata
shutil.copy('sample.txt', 'sample_copy.py')

print("Duplicate file created using shutil.copy()")
Duplicate file created using shutil.copy()

Using shutil.copyfile()

The shutil.copyfile() is similar to shutil.copy(), except that it only copies file content without metadata and permissions.

import shutil

# Create a sample file
with open('data.txt', 'w') as f:
    f.write("Sample data for copyfile demonstration.")

# Copy only file content (without metadata)
shutil.copyfile('data.txt', 'data_copy.txt')

print("Duplicate file created using shutil.copyfile()")
Duplicate file created using shutil.copyfile()

Creating Duplicate File Using open() Function

Here, we use the manual method to create a duplicate file by using the open() function with 'r' mode to read content from the source file and 'w' mode to write the same content into the duplicate file ?

Example

# Create a sample file first
with open('original.txt', 'w') as f:
    f.write("This is the original file content.\nLine 2 of the file.")

# Read the original file and create duplicate
with open('original.txt', 'r') as src:
    content = src.read()

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

print("Duplicate file created using open() function")
Duplicate file created using open() function

Creating Duplicate File Using pathlib

The pathlib module allows interaction with files and directories using an object-oriented approach. We use the read_text() function to read the entire content as a string, then write this content to the duplicate file using write_text() ?

Example

from pathlib import Path

# Create a sample file using pathlib
Path('source.txt').write_text("Content created using pathlib.\nSecond line of content.")

# Copy contents from original file to duplicate
Path('source_copy.txt').write_text(Path('source.txt').read_text())

print("Duplicate file created using pathlib")
Duplicate file created using pathlib

Comparison

Method Copies Metadata Best For
shutil.copy() Yes Complete file duplication
shutil.copyfile() No Content-only copying
open() No Manual control over process
pathlib No Modern object-oriented approach

Conclusion

Use shutil.copy() for complete file duplication including metadata. Use pathlib for modern Python file operations or open() for manual control over the copying process.

Updated on: 2026-03-24T18:09:04+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements