Search code examples
pythonshutilos.path

how to check if path exist and copy folders and files using python


I have a python script that must check first if folder exist in giving path then if exist delete the existing one then copy the new one from the source. if does't exist just copy the new folder.

the problem is that in the source path if i have folder the system will check if the folder exist in the destination and delete and copy if exist.

what i want is to just check for one folder name "test" and then if does't exist copy the new folder if exist delete and copy.

code:

import os
import shutil
from os import path
import datetime


src = "I:/"
src2 = "I:/test"

dst = "C:/Users/LT GM/Desktop/test/"
dst2 = "C:/Users/LT GM/Documents/"
dst3 = "C:/Users/LT GM/Documents/Visual Studio 2017"

def main():
    copy()

def copy():
    #go through  the src files and folders 
    for root,dirs,files in os.walk(src):
        try:
            for folder in dirs:
                #return folder name
                full_folder_name = os.path.join(src, folder)
                print("folder : ",full_folder_name)
                #check if folder exits
                if os.path.exists(dst):
                    print("folder exist")
                    #delete folder
                    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:
                    print("folder does Not Exist")
                    #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))
        except Exception as e:
            print(e)

        try:
            for name in files:
                full_file_name = os.path.join(src, name)
                print("files: ",full_file_name)
                #check for pdf extension
                if name.endswith("pdf"):
                    #copy files
                    shutil.copy(full_file_name, dst2)

                    #check for doc & docx extension 
                elif name.endswith("docx") or name.endswith("doc"):
                    #copy files
                    shutil.copy(full_file_name, dst3)
            print("word files done")
            print("pdf files done")
        except Exception as e:
            print(e)
if __name__=="__main__":
    main()

Solution

    1. why do you even check? Just rmtree the destination and ignore the error if it doesn't exist. You're not getting any significant saving by first checking, you're just making your code more complex.

    2. why do you delete src for every folder you're copying? Just delete it once before the loop

    3. also you should probably copy the sub-folders of src in sub-folders of dst rather than dump everything in src

    4. os.walk will recursively walks through all the directories under the root (and thus their directories, and theirs, ...), that really doesn't seem to be what you want here,

    5. your path management is weird as hell, why do you have two different sources and three destinations used completely inconsistently?