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.
1 Answer 1
There is a parameter: dirs_exist_ok and the default is False. Use it with True.
shutil.copytree(libEntity,newDir,dirs_exist_ok=True)
3 Comments
shutil.pyi file, dirs_exist_ok is defined only in if sys.version_info >= (3, 8), that is, Python 3.8 or later.import sys; sys.version and confirmed 3.8+.
copytreesays "The destination directory must not already exist." What is it at the moment? An empty directory?