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

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

REPL Inspect

from os import removedirs
help(removedirs)

Linked from

Related notes