0

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)
PatrickRKa
  • 21
  • 4
  • 1
    Could you explain the difference between what `dirs_exist_ok=True` does and what you want? It sounds like `dirs_exist_ok=True` does exactly what you're asking for. – user2357112 Nov 02 '22 at 11:12
  • Sure, let me just copy the explanation from the docs, I think it explains it pretty well: If dirs_exist_ok is false (the default) and dst already exists, a FileExistsError is raised. If dirs_exist_ok is true, the copying operation will continue if it encounters existing directories, and files within the dst tree will be overwritten by corresponding files from the src tree. – PatrickRKa Nov 02 '22 at 12:09
  • In addition, this means if I want to copy a folder folder_one to C:\Users\Username\Desktop\Destination_folder\subfolder and then try to copy a folder folder_two to the exact same location it will throw a file already exists error. If I set the parameter dirs_exist_ok=True it will "delete" the previously copied folder folder_one and successfully copy folder_two. – PatrickRKa Nov 02 '22 at 12:14
  • Do you want to copy folder_one to C:\Users\Username\Desktop\Destination_folder\subfolder, or to C:\Users\Username\Desktop\Destination_folder\subfolder\folder_one? – user2357112 Nov 02 '22 at 12:45
  • It sounds like you're expecting shutil.copytree to copy the source directory into a subdirectory of the destination. That's not how it works. If you tell it to copy folder `f` to destination `C:\something\whatever`, it will produce a copy at `C:\something\whatever`, not `C:\something\whatever\f`. – user2357112 Nov 02 '22 at 12:54
  • Thats not what I meant. I know that copytree will create the the subfolder and put all the folders inside the src directory into the subfolder. If I now want to put the folders from another src directory into the subfolder it wouldnt work, because it would try to create subfolder once again. But I already found a solution to the problem. I just use the internal copy_tree function in python and say from distutils.dir_util import copy_tree. This function will let both src directories land in the same destination directory. – PatrickRKa Nov 02 '22 at 13:04
  • In detail this is explained here: https://stackoverflow.com/questions/1868714/how-do-i-copy-an-entire-directory-of-files-into-an-existing-directory-using-pyth – PatrickRKa Nov 02 '22 at 13:04
  • `shutil.copytree` with `dirs_exist_ok` already does exactly that, though. It's not going to delete the stuff you copied the first time, unless both folders have a file with the same name and there's a name clash or something. If you're not seeing the stuff you copied the first time, something else is going wrong in your code. – user2357112 Nov 02 '22 at 13:14
  • If you've got files with the same names in the folders you're copying, then it's not clear what you expect to happen other than the first one getting overwritten. If there's no such name clash, then copytree isn't going to go around deleting things, and you're probably not copying what you think you're copying. – user2357112 Nov 02 '22 at 13:23
  • Actually, you are right. Honestly I got confused with which folders had to be inside my destination at some point so I must have overlooked that it was already working as expected. So both solutions work just fine. Thanks for helping me out. – PatrickRKa Nov 02 '22 at 13:25

1 Answers1

0

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

PatrickRKa
  • 21
  • 4