All TalkersCode Topics

Follow TalkersCode On Social Media

Python Copy Directory Recursively

Last Updated : Aug 5, 2023

Python Copy Directory Recursively

In this article we will show you the solution of python copy directory recursively, our os.walk() function will help us get a list of all files in a folder/directory, including its subfolders/subdirectories. As a result of the os.walk() function, one can walk through the current directory, its subfolders, and its files.

The purpose of this tutorial is to demonstrate how to get a list of files inside a directory and its subdirectories by using examples.

We will now discuss the idea of how to copy a directory recursively in python with an example.

Step By Step Guide On Python Copy Directory Recursively :-

Code 1

import os
path ="C:/TC/python"
#we shall store all the file names in this list
filelist = []
for root, dirs, files in os.walk(path):
 for file in files:
        #append the file name to the list
  filelist.append(os.path.join(root,file))
#print all the file names
for name in filelist:
    print(name)

Code 2

# Python program to demonstrate
# shutil.copytree()
# importing modules
import shutil
# Declaring the ignore function
def ignoreFunc(file):
    def _ignore_(path, names):
        # List containing names of file
        # that are needed to ignore
        ignored = []
        # check if file in names
        # then add it to list
        if file in names:
            ignored.append(file)
        return set(ignored)
    return _ignore_
# Source path
src = 'C:/TC/python/src'
# Destination path
dest = 'C:/TC/python/dst'
# Copying the contents from Source
# to Destination without some
# specified files or directories
shutil.copytree(src, dest, ignore = ignoreFunc('a'))
  1. The path variable contains the path to the folder we want to scan for files in. In this case it is "C:/TC/python".
  2. Our next step will be to create an empty list called filelist, where we will add all the names of the files we find in the folder to it.
  3. This step walks through all subdirectories and directories contained in the given path using the OS library.
  4. A loop is used to go through all the files within each directory.
  5. For each file, the directory path and the file name are combined using the os.path.join() method. By doing this, you will create the full path to the file.
  6. In this case, we append the full path to the list of files.
  7. In our final loop, we will print out all the file names that were added to the filelist.
  8. As a result, this code can be used to scan a folder and all its subdirectories quickly in order to locate files. By adding an if condition to the loop that checks the file extension, this search can also be modified to look for specific types of files.

Conclusion :-

As a result, we have successfully learned how to copy a directory recursively in python with an example.

As part of this Python Tutorial of Examples, we learned how to get a list of all files in a directory with subdirectories.

I hope this article on python copy directory recursively helps you and the steps and mentioned above are easy to follow and implement.

Latest Tutorials