Skip to main content

How to copy a file in Python

How to copy a file in Python.

Here's a step-by-step tutorial on how to copy a file in Python using different methods and code examples:

Table of Contents

  1. Introduction
  2. Using the shutil Module
  3. Using the os Module
  4. Using the open() Function
  5. Conclusion

Introduction

Copying files is a common task in any programming language, including Python. In Python, there are multiple ways to copy a file. In this tutorial, we will explore three different methods:

  1. Using the shutil module
  2. Using the os module
  3. Using the open() function

Let's dive into each method with code examples.

Using the shutil Module

The shutil module in Python provides a high-level interface for file operations. It includes functions to copy, move, and delete files. Here's how you can use the shutil module to copy a file:

import shutil

# Specify the source and destination file paths
source_file = 'path/to/source/file.txt'
destination_file = 'path/to/destination/file.txt'

# Copy the file
shutil.copy(source_file, destination_file)

In the above example, replace 'path/to/source/file.txt' with the actual path of the file you want to copy, and 'path/to/destination/file.txt' with the desired destination path.

You can also use shutil.copy2() to preserve the original file's metadata (e.g., timestamps) in the copied file.

import shutil

# Specify the source and destination file paths
source_file = 'path/to/source/file.txt'
destination_file = 'path/to/destination/file.txt'

# Copy the file with metadata
shutil.copy2(source_file, destination_file)

Using the os Module

The os module in Python provides a way to interact with the operating system. It includes functions for file and directory operations. Here's how you can use the os module to copy a file:

import os

# Specify the source and destination file paths
source_file = 'path/to/source/file.txt'
destination_file = 'path/to/destination/file.txt'

# Copy the file
os.system(f'cp {source_file} {destination_file}')

In the above example, replace 'path/to/source/file.txt' with the actual path of the file you want to copy, and 'path/to/destination/file.txt' with the desired destination path.

Note: The os.system() function executes a shell command. In this case, we're using the cp command to copy the file.

Using the open() Function

The open() function in Python is primarily used for file reading and writing. However, it can also be utilized to copy a file by reading its contents and writing them to a new file. Here's how you can use the open() function to copy a file:

# Specify the source and destination file paths
source_file = 'path/to/source/file.txt'
destination_file = 'path/to/destination/file.txt'

# Open the source file in read mode
with open(source_file, 'r') as file:
# Read the contents of the source file
file_contents = file.read()

# Open the destination file in write mode
with open(destination_file, 'w') as file:
# Write the contents to the destination file
file.write(file_contents)

In the above example, replace 'path/to/source/file.txt' with the actual path of the file you want to copy, and 'path/to/destination/file.txt' with the desired destination path.

Conclusion

Copying a file in Python can be achieved using different methods, including the shutil module, the os module, and the open() function. Choose the method that suits your needs best.

Remember to replace the source and destination file paths with the actual paths in your code. Now you have the knowledge to copy files in Python!