How to Copy Files in Python

Introduction

Whether you're reading data from files or writing data to files, understanding file operations is important. In Python, and other languages, we often need to copy files from one directory to another, or even within the same directory. This article will guide you through the process of copying files in Python using various modules and methods.

Python's Filesystem and File Operations

Python provides several built-in modules and functions to interact with the filesystem and perform file operations. The two most commonly used modules for file operations are the os and shutil modules.

The os module provides a way of using operating system dependent functionality. It includes functions for interacting with the filesystem, such as os.rename(), os.remove(), os.mkdir(), and so on.

import os

# Rename a file
os.rename('old_name.txt', 'new_name.txt')

# Remove a file
os.remove('file_to_remove.txt')

# Create a directory
os.mkdir('new_directory')

The shutil module offers a number of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating process of copying and removal of files and directories.

import shutil

# Copy a file
shutil.copy('source.txt', 'destination.txt')

# Remove a directory and all its contents
shutil.rmtree('directory_to_remove')

Note: While os module functions are efficient for simple file operations, for higher-level file operations such as copying or moving files and directories, shutil module functions are more convenient.

Now, let's dive deeper into how we can use these modules to copy files in Python.

How to Copy Files in Python

Python, being a high-level programming language, provides us with several modules to simplify various tasks. One of those tasks is copying files. Whether you want to back up your data or move it to another location, Python makes it easy to copy files and directories. Here we'll take a look at how to copy files using different built-in modules.

Using the shutil Module to Copy Files

The shutil module has methods that help in operations like copying, moving, or removing files/directories. One of its most used functionalities is the ability to copy files.

To copy a file in Python using the shutil module, we use the shutil.copy() function. This function takes two parameters: the source file path and the destination path.

Let's look at an example:

import shutil

source = "/path/to/source/file.txt"
destination = "/path/to/destination/file.txt"

shutil.copy(source, destination)

In this code snippet, we first import the shutil module. We then define the source and destination file paths. Finally, we call the shutil.copy() function with the source and destination as arguments.

Note: The shutil.copy() function will overwrite the destination file if it already exists. So, be cautious while using it!

If you want to preserve the metadata (like permissions, modification times) while copying, you can use the shutil.copy2() function. This works the same way as shutil.copy(), but it also copies the metadata.

import shutil

source = "/path/to/source/file.txt"
destination = "/path/to/destination/file.txt"

shutil.copy2(source, destination)

Using the os Module to Copy Files

Python's built-in os module is another great tool for interacting with the operating system. Among its many features, and just like shutil, it allows us to copy files. However, it's important to note that the os module doesn't provide a direct method to copy files like shutil does. Instead, we can use the os.system() function to execute shell commands within our Python script.

Here's how you'd use the os.system() function to copy a file:

import os

# Define source and destination paths
src = "/path/to/source/file.txt"
dest = "/path/to/destination/file.txt"

# Use os.system to execute the cp shell command
os.system(f'cp {src} {dest}')

After running this script, you'll find that file.txt has been copied from the source path to the destination path.

Wait! Since the os.system() function can execute any shell command, it can also be a potential security risk if misused, so always be careful when using it.

This may be a helpful way to copy files if you also need to execute other shell commands or use cp flags that aren't available with shutil's methods.

Copying Files with Wildcards

In other use-cases, you might want to copy multiple files at once. Let's say we want to copy all .txt files in a directory. We can achieve this by using wildcards (*) in our file paths. The glob module in Python can be used to find all the pathnames matching a specified pattern according to the rules used by the Unix shell.

Here's is how we'd use the glob module to copy all .txt files from one directory to another:

import glob
import shutil

# Define source and destination directories
src_dir = "/path/to/source/directory/*.txt"
dest_dir = "/path/to/destination/directory/"

# Use glob to get all .txt files in the source directory
txt_files = glob.glob(src_dir)

# Loop through the list of .txt files and copy each one
for file in txt_files:
    shutil.copy(file, dest_dir)

In this code, we use glob to get a list of all text files in our source directory, which we then iterate over and copy each one individually.

After running this script, you'll see that all .txt files from the source directory have been copied to the destination directory.

Copying Directories in Python

Copying individual files in Python is quite straightforward, as we've seen. But what if you want to copy an entire directory? While it sounds more complicated, this is actually pretty easy to do with the shutil module. The copytree() function allows you to copy directories, along with all the files and sub-directories within them.

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

import shutil

shutil.copytree('/path/to/source_directory', '/path/to/destination_directory')

This will copy the entire directory at the source path to the destination path. If the destination directory doesn't exist, copytree() will create it.

Note: If the destination directory already exists, copytree() will raise a FileExistsError. To avoid this, make sure the destination directory doesn't exist before you run the function.

Error Handling and Types of Errors

When dealing with file operations in Python, it's important to handle potential errors. There are many types of errors you might encounter, with some of the more common ones being FileNotFoundError, PermissionError, and IsADirectoryError.

You can handle these errors using Python's try/except blocks like this:

import shutil

try:
    shutil.copy2('/path/to/source_file', '/path/to/destination_file')
except FileNotFoundError:
    print("The source or destination file was not found.")
except PermissionError:
    print("You don't have permission to access the source or destination file.")
except IsADirectoryError:
    print("The source or destination path you provided is a directory, not a file.")

In this example, we're using the shutil.copy2() function to copy a file. If the source or destination file doesn't exist, a FileNotFoundError is raised. If the user doesn't have the necessary permissions, a PermissionError is raised. If the source or destination path is a directory instead of a file, an IsADirectoryError is raised. Catching each error in this way allows you to handle each case in the appropriate way.

Conclusion

In this article, we have showed different ways to copy files in Python. We saw how the Python's built-in shutil and os modules provide us with simple and powerful tools for file copying. We also showed how to employ wildcards to copy multiple files and how to copy entire directories.

And finally, we looked at a few different types of common errors that might occur during file operations and how to handle them. Understanding these errors can help you debug your code more efficiently and prevent issues, like data loss.

Last Updated: September 7th, 2023
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms