How to copy a directory and its contents to an existing location using Python?

后端 未结 4 1921
暖寄归人
暖寄归人 2020-12-23 17:04

I\'m trying to copy a directory and all its contents to a path that already exists. The problem is, between the os module and the shutil module, there doesn\'t seem to be a

4条回答
  •  盖世英雄少女心
    2020-12-23 17:24

    Are you gettting the error that says "Cannot create a directory when its already present"? I am not sure how much silly is this, but all i did was to insert a single line into copytree module: I changed :

    def copytree(src, dst, symlinks=False):
        names = os.listdir(src)
        os.makedirs(dst)
    

    into:

    def copytree(src, dst, symlinks=False):
        names = os.listdir(src)
        if (os.path.isdir(dst)==False):
            os.makedirs(dst)       
    

    I guess i did some bluder. If so, could someone point me out that? Sorry, i am very new to python :P

提交回复
热议问题