I have a code that copy folders and files from a given path that includes pdf files and word files and folder that includes pdf files also.
what i need is to copy each one to a different destination mean
- the pdf file to destination a
- the word file to destination b
- the folder & subdiretories a destination c
the problem is that the destination a will have the pdf files and the subdirectories files that are in the folder. yet the destination c also will have the folder copied correctly.
so the problem is with the destination a
This is the structure I have in I:
I:
├── file1.doc
├── file1.pdf
├── file2.pdf
└── folder1
    ├── file3.pdf
    └── file4.pdf
Result must be:
├── A
│   ├── file1.pdf
│   └── file2.pdf
├── B
│   └── file1.doc
└── C
    └── folder1
        ├── file3.pdf
        └── file4.pdf
The outcome for now is :
├── A
│   ├── file1.pdf
│   ├── file2.pdf
│   ├── file3.pdf
│   └── file4.pdf
├── B
│   └── file1.doc
└── C
    └── folder1
        ├── file3.pdf
        └── file4.pdf
And this is the code I'm using:
import os
import shutil
from os import path
src = "I:/"
src2 = "I:/test"
dst = "C:/Users/xxxx/Desktop/test/"
dst2 = "C:/Users/xxxxx/Documents/"
dst3 = "C:/Users/xxxxx/Documents/Visual Studio 2017"
def main():
    copy()
def copy():
    i = 0
    j = 0
    for dirpath, dirnames, files in os.walk(src):
        print(f'Found directory: {dirpath}')
        # for file_name in files:
        if len(dirnames)==0 and len(files)==0:
                print("this directory is empty")
        else:
            print(files)
        for name in files:
            full_file_name = os.path.join(dirpath, name)
            #check for pdf extension
            if name.endswith("pdf"):
                i=i+1
                #copy files
                shutil.copy(full_file_name, dst2)
                #check for doc & docx extension 
            elif name.endswith("docx") or name.endswith("doc"):
                j=j+1
                #copy files
                shutil.copy(full_file_name, dst3)
                # print(i,"word files done")
        print("{0} pdf files".format(i))
        print("{0} word files".format(j))
    # except Exception as e:
        # print(e)
    if os.path.exists(dst): 
        shutil.rmtree(dst)
        print("the deleted folder is :{0}".format(dst))
        #copy the folder as it is (folder with the files)
        copieddst = shutil.copytree(src2,dst)
        print("copy of the folder is done :{0}".format(copieddst))
    else:
        #copy the folder as it is (folder with the files)
        copieddst = shutil.copytree(src2,dst)
        print("copy of the folder is done :{0}".format(copieddst))
if __name__=="__main__":
    main()
 
     
    