all Python 3shutilfunction
shutil.rmtree()
directory trees
Deletes a directory with all its contents, like rm -rf. No recycle bin and no confirmation — what is removed is gone.
Syntax
shutil.rmtree(path, ignore_errors=False, onerror=None, *, onexc=None, dir_fd=None)
os.rmdir removes only an empty directory, rmtree removes any. Given a symbolic link to a directory, rmtree raises OSError instead of deleting the target — a guard against wiping someone else's data. On Windows read-only files raise PermissionError by default; pass a handler as onexc (since 3.12; the old onerror is deprecated). ignore_errors=True silently swallows every error, so a half-deleted directory goes unnoticed.
Examples
import os, shutil
os.makedirs('tmp/a/b')
open('tmp/a/b/f.txt', 'w').close()
try:
os.rmdir('tmp')
except OSError as e:
print(type(e).__name__) # → OSError — каталог не пуст
shutil.rmtree('tmp')
print(os.path.exists('tmp')) # → False
import os, shutil
os.mkdir('data')
os.symlink('data', 'link')
try:
shutil.rmtree('link')
except OSError as e:
print(type(e).__name__) # → OSError
print(os.path.isdir('data')) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy