In Python 3.8 the dirs_exist_ok
keyword argument was added to shutil.copytree()
:
dirs_exist_ok
dictates whether to raise an exception in casedst
or any missing parent directory already exists.
So, the following will work in recent versions of Python, even if the destination directory already exists:
shutil.copytree(src, dest, dirs_exist_ok=True) # 3.8+ only!
One major benefit is that it's more flexible than distutils.dir_util.copy_tree()
as it takes additional arguments on files to ignore, etc. There is also a draft PEP (PEP 632, associated discussion), which suggests that distutils
may be deprecated and then removed in future versions of Python 3.