all Python 3osfunction
os.removedirs
os — directories
Remove a directory and any parents that become empty.
Syntax
os.removedirs(...)
After removing the leaf it walks up the path removing parents that became empty, until it meets a non-empty one — and silently ignores the error there. So removedirs('a/b/c') may delete 'a' too if it ends up empty. An exception is raised only if the leaf itself could not be removed.
Examples
import os
os.makedirs('a/b/c')
os.removedirs('a/b/c')
print(os.path.exists('a')) # → False — вслед за c удалены опустевшие b и a
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy