How to recursively copy a directory in Python

In this tutorial, we will learn how to recursively copy a certain directory in Python.

We’ll be using the shutil package in Python to achieve our goal.

shutil – This is a predefined package in python. It is used to perform high-level files and folders operations. This has methods like shutil.copy() and shutil.copytree() which help in copying files and directories. The one drawback of this package is we cannot use it for copying meta-data.

shutil.copytree() – This method of shutil package helps in recursively copy an entire directory rooted at a source directory to a destination directory. It can take up to 7 arguments. The first argument is the source directory. The second argument is the destination directory where the directory will be directly copied. These are the two arguments which we’ll be needing for our program.

Copy a specific directory in Python recursively

The following is the code snippet for recursively copying a certain directory using Python.

import shutil   
     

path = 'K:/Python-dev/'  
     
  
source = path+'src'
     
   
desination = path+'dst'
     
destination = shutil.copytree(source, destination)

You may also read:

Leave a Reply

Your email address will not be published. Required fields are marked *