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 module called shutil
.
4 Ways to Copy a File With Python
- shutil.copy
- shutil.copyfile
- shutil.copy2
- shutil.copyfileobj
The shutil
module is part of the Python standard library and offers a wide range of high-level file operations for managing files. The library offers methods to copy, remove and handle files, 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 copying a file in Python using the shutil
module. At the end of the guide you can find a table that summarizes the features for each of the methods mentioned.
1. Copy a File in Python With Shutil.Copy
The shutil.copy()
method is used to copy a specified source file, along with the file’s permission mode data, 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. Aside from the file’s permission mode, shutil.copy()
doesn’t copy other metadata.
shutil.copy(src, dst, *, follow_symlinks=True)
The shutil.copy()
method:
- Preserves file permissions.
- Can have a directory be the destination.
- Doesn’t copy metadata.
- 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/')
2. Copy a File in Python With Shutil.Copyfile
The shutil.copyfile()
method is used to copy a 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)
The shutil.copyfile()
method:
- Doesn’t preserve file permissions.
- Cannot have a directory be the destination.
- Doesn’t copy metadata.
- 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')
3. Copy a File in Python With Shutil.Copy2
The shutil.copy2()
method is identical to shutil.copy()
, except that copy2()
also copies all the file metadata that it can. shutil.copy2()
can be helpful for file copying where preserving metadata is especially important.
shutil.copy2(src, dst, *, follow_symlinks=True)
The shutil.copy2()
method:
- Preserves file permissions.
- Can have a directory be the destination.
- Copies metadata.
- 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/')
4. Copy a File in Python With 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.
Using shutil.copyfileobj()
can be helpful for copying large files, as it allows for a customizable buffer size and it reads data in chunks by default to avoid uncontrolled memory consumption.
shutil.copyfileobj(fsrc, fdst[, length])
The shutil.copyfileobj()
method:
- Doesn’t preserve file permissions.
- Can have a directory be the destination.
- Doesn’t copy metadata.
- 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)
How to Pick the Right Method to Copy a File With in Python
We’ve now explored a few different ways for programmatically copying files with the shutil
module in Python.
Selecting the right file copying method from the shutil
module 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 shutil
copying methods. Remember, the only method that accepts (and works exclusively with) file objects is shutil.copyfileobj
.
+--------------------+----------------+--------------+-------------+
| Method | Preserves File | Destination | Copies |
| | Permissions | can be a dir | Metadata |
+--------------------+----------------+--------------+-------------+
| shutil.copy | ✔ | ✔ | ✗ |
| shutil.copyfile | ✗ | ✗ | ✗ |
| shutil.copy2 | ✔ | ✔ | ✔ |
| shutil.copyfileobj | ✗ | ✗ | ✗ |
+--------------------+----------------+--------------+-------------+
Frequently Asked Questions
Why use copy() in Python?
In Python, the copy()
method creates and returns a shallow copy of an object, such as a list. Using copy()
can be helpful for creating a new object with the same elements as the original object, while keeping the original object intact. This allows you to make changes to each object without affecting the other.
What is the difference between copy and copy2?
shutil.copy()
and shutil.copy2()
are both methods from the shutil
Python module that are used to copy a file in Python. Both methods work the same, except shutil.copy2()
also copies file metadata when copying, while shutil.copy()
does not.
Why do we use shutil in Python?
shutil
is a Python module that provides operations for handling files and collections of files in Python, including file copying and removal operations.