How to Copy a File With Python

Python’s shutil module offers four different ways to copy a file. Picking the right one depends on your use-case. Here’s what you need to know.

Published on Oct. 24, 2022
Two pythons intertwined with each other like exact copies.
Image: Shutterstock / Built In
Brand Studio Logo

Copying files programmatically is one of the most common tasks in day-to-day software development. We’ll explore a few different ways for copying files in Python using a library called shutil.

4 Ways to Copy a File With Python

  1. shutil.copy
  2. shutil.copyfile
  3. shutil.copy2
  4. shutil.copyfileobj

The shutil module is part of the Python’s Standard Library and offers a wide range of high-level file operations. The library offers numerous methods that can be used to copy a file depending on whether you want to copy metadata or file permissions and if the desired destination will be a directory.

We’ll cover all of the available methods for doing so. At the end of the guide you can find a table that summarizes the features for each of the methods mentioned.

 

Copy a File With Python Using Shutil.Copy

The shutil.copy() method is used to copy a specified source without the metadata to the destination file or directory and then return the path to the newly created file. The src can either be a path-like object or a string.

shutil.copy(src, dst, *, follow_symlinks=True)

Here’s what you need to know about this method:

  • It preserves file permissions.
  • The destination can be a directory.
  • It doesn’t copy metadata.
  • It doesn’t work with file objects.

 

Shutil.Copy Example

import shutil

# Copy file example.txt into a new file called example_copy.txt

shutil.copy('example.txt', 'example_copy.txt')

# Copy file example.txt into directory test/

shutil.copy('example.txt', 'test/')

More on Python: 11 Best Python IDEs and Code Editors Available

 

Copy a File With Python Using Shutil.Copyfile

The shutil.copyfile() method is used to copy the source file without the metadata to the specified destination file. Again, the src can either be a path-like object or a string.

shutil.copyfile(src, dst, *, follow_symlinks=True)

Here’s what you need to know about this method:

  • It doesn’t preserve file permissions.
  • The destination can’t be a directory.
  • It doesn’t copy metadata.
  • It doesn’t work with file objects.

 

Shutil.Copyfile Example

import shutil

# Copy file example.txt into a new file called example_copy.txt

shutil.copyfile('source.txt', 'destination.txt')

 

Copy a File With Python Using Shutil.Copy2

The shutil.copy2() method is identical to shutil.copy() except that copy2() attempts to preserve file metadata as well.

shutil.copy2(src, dst, *, follow_symlinks=True)

Here’s what you need to know about this method:

  • It preserves file permissions.
  • The destination can be a directory.
  • It copies metadata
  • It doesn’t work with file objects.

 

Shutil.Copy2 Example

import shutil

# Copy file example.txt into a new file called example_copy.txt

shutil.copy2('example.txt', 'example_copy.txt')

# Copy file example.txt into directory test/

shutil.copy2('example.txt', 'test/')

 

Copy a File With Python Using Shutil.Copyfileobj

If you have to work with file objects, then shutil.copyfileobj is the method to use. This method will copy the contents of the source file object to the specified destination file-like object. You can also set the length that corresponds to the buffer size used to copy the contents.

shutil.copyfileobj(fsrc, fdst[, length])

Here’s what you need to know about this method:

  • It doesn’t preserve file permissions.
  • The destination can’t be a directory
  • It doesn’t copy metadata.
  • It can work with file objects.

 

Shutil.Copyfileobj Example

import shutil

source_file = open('example.txt', 'rb')

dest_file = open('example_copy.txt', 'wb')

shutil.copyfileobj(source_file, dest_file)
Move, copy, overwrite files in Python using Python Shutil. | Video: PyMoondra

More on Python: 13 Python Code Snippets You Need to Know

 

How to Pick the Right Method to Copy a File With Python

We’ve now explored a few different ways for programmatically copying files with Python offered by the shutil module that comes as part of its standard library of the language.

Selecting the right method will depend on your specific use-case. Do you want to copy file permissions or the metadata? Do you need to copy a file to a directory, or copy something that includes file objects? Answering those questions will help you determine what method you need to use.

The table below summarizes the capabilities of each of the methods. Remember, the only method that accepts file objects is shutil.copyfileobj.

+--------------------+----------------+--------------+-------------+

|       Method       | Preserves File |  Destination |    Copies  |

|                    |  Permissions   | can be a dir |   Metadata  |

+--------------------+----------------+--------------+-------------+

| shutil.copy        |        ✔       |      ✔       |      ✗      |

| shutil.copyfile    |        ✗       |      ✗       |      ✗      |

| shutil.copy2       |        ✔       |      ✔       |      ✔      |

| shutil.copyfileobj |        ✗       |      ✗       |      ✗      |

+--------------------+----------------+--------------+-------------+   
Explore Job Matches.