Copy Files in Python: Using shutil, os, and subprocess Modules

Copy Files In Python

Python is an advanced programming language that provides many ways to automate tasks including interacting with system files when necessary but there is a risk of losing the data contained in the file, so it is essential to ensure the safety of the original file. For this, it is recommended to make a copy of it before manipulating it.

For copying a file in Python, we will use four different modules, shutil, os, and subprocess, so that you not only understand copying files but also explore new modules and methods that will help you broaden your knowledge of Python. Let’s start with the shutil module.

Copy a File in Python using shutil Module

The shutil offers easy-to-use methods for high-level operations on files and collections of files. It provides multiple functions that support file copying, deleting and overwriting. One of its functions is the copy() function which can be used to copy files.

shutil.copy()

The shutil.copy() creates a new file at the specified location containing the original file’s content that we want to copy. Additionally, it also maintains the original file permissions and timestamps.

Syntax:

shutil.copy(src, dst)

Where src is the source file to be copied and dst is the location having a file name where the content will be copied.

Example:

Below is an example of copying a file using shutil.copy() function.

# Import Module
import shutil

# Source and destination
src = './sample.txt'
dst = './new.txt'

# Copy File
shutil.copy(src, dst)

# Read the contents of the copied file
with open(dst, 'r') as file:
    contents = file.read()
    print(contents)

In the examples above and in the rest of this tuitorial, we have a file called sample.txt in the current working directory where we have written: “Hello World!”. We have used the open() and read() functions in all examples to read the contents of the copied file to verify whether the contents have been copied successfully or not.

Output:

Hello World!

More methods from the shutil Module to Copy Files

The shutil module provides many more methods in Python to copy files, let’s look at them one by one.

  1. shutil.copy2(src, dst): This is a modified form of shutil.copy(), it also copies the file metadata with contents from the source file to the destination file, although the rest works the same.
  2. shutil.copyfile(src, dst): This method is used when there is no need to copy the metadata of the file.
  3. shutil.copyfileobj(src, dst): This method uses file objects as source and destination instead of regular paths.
  4. shutil.copytree(src, dst): This method copies not only the contents of a file but also any subdirectories(folders) and their files to the destination path.

Copy a File in Python using os Module

Python provides an os module that is used for lower-level operating system interactions. It has two functions that can be used to copy the contents of a file, popen() and system(). But Python documentation mentions that os.popen() is deprecated, so we will use its system() function for copying files. 

os.system()

The function can directly execute the command passed as an argument to the system shell. Its return value depends on the OS running the program. For Linux, it is the exit status, while for Windows it is the return value by the system shell.

We can use this method to copy a file from one source directory to the destination directory by executing a relevant command. Let’s see how copying files using this method.

Syntax:

os.system(command)

where command can be any command represented as a string that you want to execute in the system shell.

Example:

# Import Module
import os

# Source and destination
src = './sample.txt'
dst = './new.txt'

# Check the operating system and use the respective command
if os.name == 'nt':  # Windows
    cmd = f'copy "{src}" "{dst}"'
else:  # Unix/Linux
    cmd = f'cp "{src}" "{dst}"'

# Copy File
os.system(cmd)

# Read the contents of the copied file
with open(dst, 'r') as file:
    contents = file.read()
    print(contents)

Here we use an additional if/else block to check the OS in order to create the command to copy the contents of the file accordingly.

Output:

Hello World!

Here we get the content as the content of the source file, so it means we have successfully copied the file.

Copy a File in Python using subprocess Module

The subprocess module in Python is used to create and manage subprocesses. We can use it to execute system commands and interact with them for further operation. It has a method call() which fulfils our requirement for copying files.

subprocess.call()

This function is similar to the os.system() function. Here we also execute a command to copy the file as we have done earlier using os.system() function.

Syntax:

subprocess.call(arg)

Where arg is a sequence of strings, the first element is considered a command, and subsequent elements are arguments.

Example:

# Import Module
import subprocess
import os

# Source and destination
src = './sample.txt'
dst = './new.txt'

# Check the operating system and use the respective command
if os.name == 'nt':  # Windows
    arg = ['copy', src, dst]
else:  # Unix/Linux
    arg = ['cp', src, dst]

# Copy File
subprocess.call(arg)

# Read the contents of the copied file
with open(dst, 'r') as file:
    contents = file.read()
    print(contents)

Here we have also implemented an if/else block to identify the OS to initialise the command for copying the file’s contents since each OS has a different command. Also, if you look closely, the command is not a string(like in os.system() function), it is a list of arguments where “copy” or “cp” is considered the command, followed by the source file(src) and the destination file(dst).

Output:

Hello World!

Summary

In this tuitorial, we have learned three functions for the Python copy file and directories: shutil.copy() of the shutil module, os.system() of the os module, and subprocess.call() of the subprocess module. All of them can be used for copying files, but the one we recommend most is the shutil.copy() function.

References