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)
asked Nov 2, 2022 at 11:09
10
  • 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. Commented Nov 2, 2022 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. Commented Nov 2, 2022 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. Commented Nov 2, 2022 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? Commented Nov 2, 2022 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. Commented Nov 2, 2022 at 12:54

1 Answer 1

0

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

answered Nov 4, 2022 at 13:18
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.