pythonpython-3.xshutilcopyingcopytree

Alternative for python shutils.copytree?


recently i built a tool, which is able to copy files and directories to a selected location. I used the shutil.copy function for copying files and the shutil.copytree function for copying directories. It all worked fine until i stumbled upon the following problem:

shutil.copytree function takes two arguments: src and dst. instead of looking at the destination directory and copying src to that location it will always try to create the whole path leading to dst again. therefore it will always return a file exists error, when dst already exists (no matter if it existed before or copytree created it in a previous action) since it can not overwrite existing directories by its default settings.

So when I want to copy two directories into the same destination directory with shutil.copytree it will not work. Is there a different function I could use or a specific workaround that would be helpful in my situation?

Thanks in advance :)


Solution

  • The parameter "dirs_exist_ok=" should be set to "True" this allows for the existance of Directories at the desired location.