In this article we will explore how to copy files from one directory to another using Python.

Table of Contents



Introduction

This article aims to provide a comprehensive guide on how to copy files from one directory to another using Python.

Whether you need to copy individual files, replicate an entire directory, or automate file copying tasks, Python has the tools you need.

To effectively copy files using Python, it’s beneficial to have a basic understanding of file handling concepts, including:

  • File Paths: Understanding how file paths work is crucial. You should be comfortable specifying the paths to source and destination files or directories.
  • File I/O: Knowledge of file input and output operations, such as reading and writing files, can be helpful but is not strictly required for basic file copying tasks.
  • Exception Handling: Familiarity with handling exceptions (errors) that may occur during file operations will allow you to write robust and reliable code.

In this tutorial we will work with the os and shutil (“shell utility”) modules. os module provides a portable way of using operating system dependent functionality, and shutil module that provides a wide range of file operations. Both modules are a part of the standard library, so there is nothing additional to install.


Sample files

Here are the 3 sample files we will use in this tutorial:

and we will place them in the sample_files folder. We will also create an empty folder and call it new_folder. Now your directory with the code file should look like this:

sample file structure

Copy a single file

In this section, we’ll explore how to copy a single file from one directory to another using Python.

This is one of the simplest operations when dealing with file management tasks, and Python makes it straightforward with the shutil module:


#Import the required dependencies
import os
import shutil

#Define path to folder where file is located
source_directory = 'sample_files/'

#Define path to folder to which the file will be copied
destination_directory = 'new_folder/'

#Define the file name
file_name = 'sample_file1.txt'

#Get the relative full path to file
source_path = os.path.join(source_directory, file_name)

#Create the new path to file, to which it will be copied
destination_path = os.path.join(destination_directory, file_name)

#Copy file from one directory to another
shutil.copy(source_path, destination_path)
print(f'File copied from {source_path} to {destination_path}')

You should see the following message printed:

File copied from sample_files/sample_file1.txt to new_folder/sample_file1.txt

and you should see the copied file in the destination directory:

copy single file using python

Copy multiple files

In this section, we’ll explore how to copy multiple files from one directory to another using Python.

This is especially useful when you need to replicate multiple files from a source folder to a destination folder.

The code will be 90% the same as in the previous section, except now we will iterate through a list of files and copy them one by one.


Note: In previous section we copied one file (sample_file2.txt) from sample_files to new_folder. Let’s delete it from the new_folder, so we have the original file structure:

sample file structure

Now, to copy multiple files, we are going to create a list of file names that we want to copy and then use a loop to iterate through the list, generate file paths, and copy each file one at a time:


#Import the required dependencies
import os
import shutil

#Define path to folder where file is located
source_directory = 'sample_files/'

#Define path to folder to which the file will be copied
destination_directory = 'new_folder/'

#Define the file names
file_names = ['sample_file1.txt', 'sample_file2.txt']

#Iterate over files
for file_name in file_names:

    #Get the relative full path to file
    source_path = os.path.join(source_directory, file_name)

    #Create the new path to file, to which it will be copied
    destination_path = os.path.join(destination_directory, file_name)

    #Copy file from one directory to another
    shutil.copy(source_path, destination_path)
    print(f'File copied from {source_path} to {destination_path}')

You should see the following message printed:

File copied from sample_files/sample_file1.txt to new_folder/sample_file1.txt
File copied from sample_files/sample_file2.txt to new_folder/sample_file2.txt

and you should see the copied files in the destination directory:

copy multiple files using python

Copy a directory

In this section, we’ll learn how to copy an entire directory, including its subdirectories and files, from one location to another using Python.

This operation is useful when you need to duplicate an entire folder structure along with its contents.

Note: In previous section we copied multiple files (sample_file1.txt and sample_file2.txt) from sample_files to new_folder. Let’s delete it the entire new_folder directory, so we have the just the directory with sample files and Python code:


To copy an entire directory from one location to another (including all of its subdirectories and files), we will use the shutil.copytree() function:


#Import the required dependency
import shutil

#Define path to source folder
source_directory = 'sample_files/'

#Define path to destination folder
destination_directory = 'new_folder/'

#Copy the entire directory
shutil.copytree(source_directory, destination_directory)
print(f'Directory copied from {source_directory} to {destination_directory}')

You should see the following message printed:

Directory copied from sample_files/ to new_folder/

and you should see the copied files in the destination directory:

Note: if the destination directory already exists (in our case if the new_folder directory existed, even empty), shutil.copytree() will raise a FileExistsError.


Conclusion

In this article explored how to copy files from one directory to another using Python with os and shutil modules.

Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Python for File Handling tutorials.