How to Copy Files in Python

To copy files in Python, use the shutil.copyfile() method. The shutil.copyfile() method copies the source file’s content to the destination file. The metadata of the file is not copied. Source and destination must represent a file, and destination must be writable.

To work with files in Python, file handling is the prerequisite to play with the files in Python. In addition, file handling plays a vital role in storing the data permanently into a file at a specific location so that we can access the data or the file (for editing, modifying, deleting, copying, and many more) later, even after the program terminates.

This article is divided into two parts

  • How to copy a single file in Python?
  • How to copy multiple files in Python?

In this article, we will be majorly working with the “shutil” module because it provides many functions on a single file and multiple files in Python.

There are three basic functions to copy file(s), which are provided by the “shutil” module, which are:

  1. copyfile(): This function copies the contents of a file.
  2. copy(): This function copies the content of a file with permission to copy to the destination directory.
  3. copy2(): This function copies the content of a file with permission to copy it into the destination directory. It copies the metadata of that file, including the files creation and modification time.

How to copy a single file in Python

To copy a single file in Python, use the copyfile() function. For example, the shutil.copyfile() function copies the source file’s content to the destination file.

Step 1: Import the shutil module

The shutil is a built-in Python module that provides many functions of high-level operations on files and collections of files. 

import shutil

Step 2: Use a copyfile() function.

shutil.copyfile(source, destination)

The copyfile() has two parameters: the file’s source and destination.

So, I have a plain text file within my source folder called Friendz.txt.

Which says,

“My friends are Aarti, Arnav, Medha, Priyanka, Shubham, Sumit”

shutil.copyfile('Friendz.txt','Copy_Friendz.txt')

If you are in the same folder, then there is no need to provide the full path of the file; you can write the file’s name.

My file name is “Friendz.txt,” and The destination file is “Copy_Friendz.txt”

Even if the destination file doesn’t exist, Python will automatically create a text file.

And hence by using the “copyfile()” function, all the content from Friendz.txt” is copied to “Copy_Friendz.txt”.

How to copy multiple files in Python

To copy multiple files in Python, use the copy() method. The shutil.copy() is a built-in Python method that is used to copy the content of the source file to the destination file or directory.

Syntax

shutil.copy(source, destination, *, follow_symlinks = True)

Arguments

  1. source: It is a string representing the path of the source file. 
  2. destination: It is a string representing the path of the destination file or directory. 
  3. follow_symlinks (optional) : The default value of this parameter is True.

Let’s see an example step by step.

Step 1: Import shutil and glob module.

import shutil
import glob

Step 2: Initialize a variable with the source directory path

sourcePath = r"C:\Users\LENOVO\Downloads\Example\Source"

STEP 3: Use the “glob()” function to get the paths of the files in a variable.

filePaths = glob.glob(sourcePath + "//*.txt")

STEP 4: Initialize a variable with the destination directory path.

destPath = r"C:\Users\LENOVO\Downloads\Example\Destination"

STEP 5:

for path in filePaths:
   fileName = path.split("""\\""")[-1]
   destFilePath = destPath + """\\""" + fileName
   shutil.copy(path, destFilePath)

In this example, we used for loop to access all the files. Initially, we are splitting the files and then updating the destination file path, and then using the “copy()” function, we are copying the files from source to destination.

Final words

In this article, we learned how to copy a single file? And how to copy multiple files?

The “shutil” module provides us with three functions to copy,

  1. copyfile(): copies contents of a file.
  2. copy(): copyfile() + permission mode + destination can be a directory.
  3. copy2(): copy() + copies metadata (file’s creation and modification times).

That’s it for this tutorial.

Related posts

How to Read CSV File in Python

How to Import File in Python

How to Save Array to File in Python

Leave a Comment