all Python 3shutilfunction
shutil.copytree()
directory trees
Recursively copies a directory with all its contents and returns the copy's path. Files are copied with copy2 by default.
Syntax
shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False) -> dst
dst must not exist — otherwise FileExistsError; dirs_exist_ok=True (since 3.8) merges into an existing directory, overwriting files with the same names. dst is the new directory itself, not a place to put src into: copytree('a', 'b') gives b with a's contents, not b/a. Symbolic links are followed by default (symlinks=False) — the targets get copied. Errors on individual files are collected and raised at the end as a single shutil.Error.
Examples
import os, shutil
os.makedirs('src/sub')
open('src/sub/f.txt', 'w').close()
print(shutil.copytree('src', 'dst')) # → dst
print(os.listdir('dst')) # → ['sub']
try:
shutil.copytree('src', 'dst')
except FileExistsError as e:
print(type(e).__name__) # → FileExistsError
shutil.copytree('src', 'dst', dirs_exist_ok=True)
print(os.path.exists('dst/sub/f.txt')) # → True
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy