On this page
shutil.copytree
Prev: shutil.copystat | Up: shutil | Next: shutil.disk_usage
Rel: shutil.chown, shutil.copy, shutil.copy2, shutil.copyfile
Ref: docs.python.org — shutil.copytree
Tags: #public #python3 #stdlib #shutil
Import
import shutil
from shutil import copytree
Signature & Overview
shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=<function copy2 at 0x1093e0510>, ignore_dangling_symlinks=False, dirs_exist_ok=False)
Recursively copy a directory tree and return the destination directory.
Usage
from pathlib import Path
import shutil
import tempfile
with tempfile.TemporaryDirectory() as tmp:
src, dst = Path(tmp) / "src", Path(tmp) / "dst"
src.mkdir()
(src / "app.py").write_text("x = 1", encoding="utf-8")
(src / "temp.pyc").write_text("skip", encoding="utf-8")
shutil.copytree(src, dst, ignore=shutil.ignore_patterns("*.pyc"), dirs_exist_ok=True)
assert (dst / "app.py").exists() and not (dst / "temp.pyc").exists()
Gotchas
- Pass
dirs_exist_ok=Trueto merge into an existing directory: Without it,copytree()raisesFileExistsErrorifdstalready exists. - Avoid copying a directory into itself: Copying
srcintosrc/subdircauses infinite recursion unless excluded viaignore=.
REPL Inspect
from shutil import copytree
help(copytree)
Linked from
Related notes
- tuts/python3/stdlib_module/shutil/copystat Linked here; links to this page; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/disk_usage Linked here; links to this page; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/shutil/chown Linked here; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copy Linked here; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copy2 Linked here; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copyfile Linked here; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copyfileobj shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copymode shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/Error shared tags: #shutil, #stdlib