Alternative for python shutils.copytree?

Question:

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 🙂

  • I tried setting shutil.copytrees parameter dirs_exist_ok= True, hoping that it would not follow its regular behaviour and ignore the fact, that the destination directory already existed. Instead it has just overwritten the previously copied directory (so it deleted the directory that was copied first)
Asked By: PatrickRKa

||

Answers:

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

Answered By: PatrickRKa