On this page
os.removedirs
Prev: os.remove | Up: os | Next: os.rename
Rel: os.abort, os.access, os.chdir, os.chmod
Ref: docs.python.org — os.removedirs
Tags: #public #python3 #stdlib #os
Import
import os
from os import removedirs
Signature & Overview
os.removedirs(name)
Super-rmdir; remove a leaf directory and all empty intermediate ones. Works like rmdir except that, if the leaf directory is successfully removed, directories corresponding to rightmost path
Usage
import os
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
leaf = Path(tmp) / "x" / "y"
leaf.mkdir(parents=True)
os.removedirs(leaf)
assert not (Path(tmp) / "x").exists()
Gotchas
- Removes empty directories upward until a non-empty parent is hit: Stops silently once a parent is non-empty; fails on the leaf if the leaf itself isn't empty.
REPL Inspect
from os import removedirs
help(removedirs)
Linked from
Related notes
- tuts/python3/stdlib_module/os/remove Linked here; links to this page; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/rename Linked here; links to this page; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/os/abort Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/access Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chdir Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chmod Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chown shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/close shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/cpu_count shared tags: #os, #stdlib