0

I am trying to achieve the following the following in python:

I need to be able to copy and entire subfolder contents to another folder. However, i am not able to define the source path as the name of the main folder would change.

Source: C:\user\Week1\day1\DesiredFolder
        C:\user\Week1\day2\DesiredFolder

Destination: C:\user\Output\DesiredFolderContents

In this case i should be able to copy the desired folder contents to the destination folder.

I am a novice to python and any help would be most appreciated.

Thank you!

Please find my code below.Thank you for your help

import os
import shutil
fullpath = os.path.join
start_directory2 = "D:\\RECS GRAPHS\\GRAPHS WEEKLY\\WEEK 2\\"     
desiredfolder2="D:\\RECS GRAPHS\\WEEKLY DATA\\WEEK 2\\DesiredFolder\\"


def main():
    for dirname,dirnames, filenames in os.walk(start_directory2):
        for filename in dirnames:
            source = fullpath(dirname,filename)
            if dirname.startswith("DesiredFolder_Name"):
                shutil.copytree(source,desiredfolder2)   

if __name__ == "__main__": 
    main()
vurmux
  • 9,420
  • 3
  • 25
  • 45
Sona123
  • 91
  • 4
  • 13
  • How many source folders do you have? – vishal Aug 27 '18 at 05:18
  • Possible duplicate of [How to copy directory recursively in python and overwrite all?](https://stackoverflow.com/questions/12683834/how-to-copy-directory-recursively-in-python-and-overwrite-all) – randomir Aug 27 '18 at 05:25
  • @serbia99 The idea is to copy all the folders named"Desired Folder" within the main folder. To explain further Week1 has 5 folders-for each business day of the week and each day has the "Desired Folder"subfolder which needs to be copied.Thank you – Sona123 Aug 27 '18 at 05:32
  • Please post the code you have tried so far – vishal Aug 27 '18 at 05:46
  • Edited.Thank you! – Sona123 Aug 27 '18 at 06:13

0 Answers0