Trying to use copytree to copy over subfolders recursively in Python

195 Views Asked by At

I'm working on a code that will copy over subfolders of certain directories to a new directory. I feel like I've tried everything but I can't seem to get them to copy. Only the folder and files the subfolder is located in gets copied over. Below is my code so far:

for cust in MULTIPLE_FOLDERS_LIST:
      if cust in clientName:
         for root, dirs, files in os.walk('\\'.join(configDir.split('\\'))):
            for direc in dirs:
               if cust in direc and not os.path.exists('\\'.join(customerConfigCustomFolderPath.split
('\\'))+'\\'+direc):
                  shutil.copytree('\\'.join(configDir.split('\\'))+'\\'+direc, '\\'.join(customerConfigCustomFolderPath.split('\\')[:-1])+'\\'+direc)

The locations seem to be correct (have to target the directories relatively). If anyone has any ideas it would be greatly appreciated!

1

There are 1 best solutions below

1
On

The entire purpose of copytree is to handle the directory walking for you - there is no need to os.walk yourself.

To filter what is copied, use the ignore parameter as described in the documentation.