Elegant way to make all dirs in a path

You are looking for os.makedirs() which does exactly what you need.

The documentation states:

Recursive directory creation function.
Like mkdir(), but makes all
intermediate-level directories needed
to contain the leaf directory. Raises
an error exception if the leaf
directory already exists or cannot be
created.

Because it fails if the leaf directory already exists you’ll want to test for existence before calling os.makedirs().

Leave a Comment

tech