On this page
shutil.rmtree
Prev: shutil.register_unpack_format | Up: shutil | Next: shutil.SameFileError
Rel: shutil.chown, shutil.copy, shutil.copy2, shutil.copyfile
Ref: docs.python.org — shutil.rmtree
Tags: #public #python3 #stdlib #shutil
Import
import shutil
from shutil import rmtree
Signature & Overview
shutil.rmtree(path, ignore_errors=False, onerror=None, *, onexc=None, dir_fd=None)
Recursively delete a directory tree.
Usage
from pathlib import Path
import shutil
import tempfile
tmp = Path(tempfile.mkdtemp())
(tmp / "nested").mkdir()
(tmp / "nested" / "file.txt").write_text("bye", encoding="utf-8")
shutil.rmtree(tmp)
assert not tmp.exists()
Gotchas
- Irreversible recursive delete: Always validate the target path before calling
rmtree()—it does not use the OS Trash/Recycle Bin. - Read-only files on Windows:
rmtree()can raisePermissionErroron read-only.gitfiles on Windows unless you pass anonexc=(Python 3.12+) /onerror=handler that clears the read-only bit.
REPL Inspect
from shutil import rmtree
help(rmtree)
Linked from
Related notes
- tuts/python3/stdlib_module/shutil/register_unpack_format Linked here; links to this page; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/SameFileError 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/copystat shared tags: #shutil, #stdlib