How to Copy Files in Python

How to copy files in Python? Python provides a variety of ways to work with files, including copying them. In this article, we will explore the different methods for copying files in Python with examples. It’s essential to choose the right function depending on the requirements of the task at hand.

In some cases, it is necessary to preserve the original file’s metadata, such as file permissions and timestamps, while in other cases it may not be necessary. Python provides different functions for copying files that allow for preserving or ignoring metadata.

1. Quick Examples

The following examples will give you a high-level overview of different methods to copy files in python.


# Quick Examples of copying files in Python

src_path='SparkByExamples/file.txt'
dest_path='SparkByExamples/newfilefile.txt'

# Copies the file's permissions
import shutil
shutil.copy(src_path,dest_path)

#  Preserves the file's metadata
shutil.copy2(src_path,dest_path)

# It doesn't copy the permissions or metadata.
shutil.copyfile(src_path,dest_path)

# Copy the contents of a file-like object
shutil.copyfileobj(file, file_like_object, length=16*1024)

2. Python Copy Files using shutil Module

The shutil module provides a number of high-level operations on files and collections of files. In particular, it offers a function shutil.copy() and its variants shutil.copy2(), shutil.copyfile() and shutil.copyfileobj() to copy files in Python.

2.1 Copy Files without Meta Data using copyfile()

The copyfile() function is used to copy the contents of one file to another in Python. It does not copy metadata or permissions, and the destination file cannot be a directory. Here, both the source and destination have to be a filename.

Syntax:


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

Parameters:

  • src (str): The source file path.
  • dst (str): The destination file path.
  • follow_symlinks (bool): If set to False, it will copy the symbolic link instead of the file it points to.

# Copy file
import shutil
shutil.copyfile('sourcefile.txt', 'destinationfile.txt')

# Copy with full path
shutil.copyfile('/src/file/path/sourcefile.txt', '/dest/file/path/destinationfile.txt')

Important Points to Note while Copying Files:

  • It copies the contents from a file named sourcefile.txt to a file named destinationfile.txt.
  • The destination location must be writable; If a destination folder or file already exists with read-only permission, an IOError exception will be raised.
  • If destinationfile.txt already exists, it will be replaced.
  • By using this method you can’t copy files such as characters or block devices and pipes.
  • When the complete path is not specified it copies file from the current directory.

2.2 Copy Files with Meta Data using copy2()

This copy2() is used to copy a file from one location to another and preserves the file’s metadata, such as timestamp and permissions. The syntax and the parameter list are very much similar to the shutil.copyfile() method.

Note that it allows dst to be a directory, when used as a directory it copies the file with basename of src.


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

Below is an example of using shutil.copy2() function.


# Import
import shutil

# Copies file wih meta data
shutil.copy2('sourcefile.txt', 'destination.txt')

# Copies the file to another destination directory
shutil.copy2('/src/path/sourcefile.txt', '/dst/path/destination.txt')

# Copies the same file to the distination directory
shutil.copy2('/src/path/sourcefile.txt', '/dst/path/')

2.3 Copy only with Permission Data using copy()

The copy() function is used to copy a file from one location to another, it does not copy metadata but preserves file permissions.

Syntax:


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

See the following example of using the shutil.copy() function.


# Copy files using copy()
import shutil
with open('sourcefile.txt', 'rb') as src_file, open('destfile.txt', 'wb') as dst_file:
    shutil.copyfileobj(src_file, dst_file)

2.4 shutil.copy() vs shutil.copy2() vs shutil.copyfile()

Below are the comparisons between these three methods. Make sure you choose the right one while working on your project to copy files in Python.

MethodCopies MetadataCopies PermissionsUses File ObjectDestination May Be Directory
shutil.copy()YesYesNoYes
shutil.copyfile()NoNoNoNo
shutil.copy2()YesYesNoYes
copy() vs copy2() vs copyfile()

In terms of speed, the speed difference between these methods would be negligible for small to medium-sized files.For large files, or when working with many files at once.

  • shutil.copyfile(): This method is the fastest as it only copies the contents of a file without preserving metadata.
  • shutil.copy(): This method preserves the metadata of the original file, but it’s slightly slower than shutil.copyfile().
  • shutil.copy2(): This method preserves both the file and the metadata, including timestamps, which makes it slower than shutil.copy()
MethodSpeed
shutil.copyfile()Fast
shutil.copy()Medium
shutil.copy2()Slow
Comparison of the speed

3. open() and write() to copy files

The open() function is used to open a file and return a file object, which is a Python object that represents the file. The write() function is used to write data to a file. We can combine both of them to copy files from source to destination in Python.


# Open the source file in read mode
with open("source_file.txt", "r") as src_file:
    # read the contents of the source file
    src_data = src_file.read()

# Open the destination file in write mode
with open("destination_file.txt", "w") as dst_file:
    # write the contents of the source file to the destination file
    dst_file.write(src_data)

4. Copy both folders and files in python

Python provides different built-in and third-party modules to copy a single file or an entire folder. The first method is using the built-in shutil.copytree() method, and the second is using shutil.copy2() of shutil.copy() the method in FOR Loop.

4.1 Example – shutil.copytree()

In the below example, source_folder is the folder you want to copy and destination_folder is the new folder to which you want to copy the contents of the source folder.


# Copy directory
import shutil
shutil.copytree("source_dir", "destination_dir")

4.2 Example – For loop with shutil.copy2()

If you want to copy only the files in a folder and not the subfolders and its content, you can use shutil.copy() function instead, this will copy all files in the folder to the destination.


# Copy only files not directories
import os
import shutil
def copy_files(src, dst):
    for item in os.listdir(src):
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if os.path.isfile(s):
            shutil.copy2(s, d)

copy_files("source_folder", "destination_folder")

5. Copy Files using OS module

The OS is a standard utility module that provides several functions to interact with the operating system. We can use two functions popen() and system() to copy the files.

5.1 Using popen()

Following is the syntax of popen()


# Syntax
os.popen(cmd[, mode[, bufsize]])

Here is an example using popen() to copy files.


# Example to copy files using popen()
import os

# In Unix/Linux
os.popen('cp sourcefile.txt destinationfile.txt') 

# In Windows
os.popen('copy sourcefile.txt destinationfile.txt')

5.2 Using system()

Following is the syntax of the os.system()


# Syntax
os.system(command)

Here is an example using popen() to copy files.


# Example to copy files using popen()
import os

# In Linux/Unix
os.system('cp sourcefile.txt destinationfile.txt')  

# In Windows
os.system('copy sourcefile.txt destinationfile.txt')

6. Copying files using subprocess Module

The Python subprocess module is used to launch child processes from Python code, It can be used to run the shell commands for UNIX and dos commands from windows.

6.1 Syntax of subprocess

Following is the syntax of the subprocess.call()


# Syntax
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

6.2 Examples

Following is an example of running a copy command using subprocess.call() to copy a file. based on OS you are running this code, you need to use the right command. For example, cp command is used in UNIX and copy is used in winds to copy files.


# Import
import subprocess

# Example using subprocess.call()
# In Linux/Unix
status = subprocess.call('cp sourcefile.txt destinationfile.txt', shell=True) 

# In Windows
status = subprocess.call('copy sourcefile.txt destinationfile.txt', shell=True)

7. Conclusion

We have discussed different methods for copying files in python. By Now you will have a clear understanding of how to move files with metadata and without metadata. Additionally, we compared the different approaches. So make sure you choose the right one that can meet your goals. Let me know if you have any questions.

References

AlixaProDev

I am a software Engineer with extensive 4+ years of experience in Programming related content Creation.

Leave a Reply