5

I'm trying to use shutil.copytree(libEntity,newDir) in my python program and I'm getting the following error: FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:\Users\newDir

Basically it complains that the destination directory newDir already exists.

I thought that was the purpose of using shutil.copytree? I'm using Python 3.8.5 on Windows 10

I also tried 'distutils.dir_util.copy_tree(libEntity,newDir)' but this doesn't work correctly. It copies all of the files from the source directory but it does not preserve directory structure in the destination folder, rather it creates the the destination folder (newDir) and puts all of the source files in the destination folder without the creating the directory tree. This is not helpful.

Any suggestions?

Thanks.

asked Aug 30, 2020 at 20:45
2
  • 1
    Suggestions to do what exactly? The doc for copytree says "The destination directory must not already exist." What is it at the moment? An empty directory? Commented Aug 30, 2020 at 20:48
  • great tutorial to follow google.com/amp/s/www.geeksforgeeks.org/… Commented Aug 30, 2020 at 20:56

1 Answer 1

7

There is a parameter: dirs_exist_ok and the default is False. Use it with True.

shutil.copytree(libEntity,newDir,dirs_exist_ok=True)
answered Aug 30, 2020 at 20:57
Sign up to request clarification or add additional context in comments.

3 Comments

I get the error :TypeError: copy_tree() got an unexpected keyword argument 'dirs_exist_ok' when I add the dir_exist_ok part
It is likely that you are using Python 3.7 or earlier. From the shutil.pyi file, dirs_exist_ok is defined only in if sys.version_info >= (3, 8), that is, Python 3.8 or later.
While likely, it seems not always the case. While getting this error, I also checked python version via import sys; sys.version and confirmed 3.8+.

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.