On this page
  1. shutil.copytree
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

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

REPL Inspect

from shutil import copytree
help(copytree)

Linked from

Related notes