Python Function Reference

os.makedirs

Create a directory together with all missing parent directories.

Python 3.13
os.makedirs(name, mode=0o777, exist_ok=False)
  • =default value
name
The path to the directory, such as data/2026/may.
str | PathLike
mode
The permissions of the last directory in the path, 0o777 by default.
intOptional
exist_ok
If True, an existing directory is not treated as an error.
boolOptional

Return value

Nothing (None); raises FileExistsError if the directory exists and exist_ok is False.

Examples

Nested directories in one call

Python 3.13
import os
os.makedirs('data/2026/may', exist_ok=True)
print(os.path.exists('data/2026/may'))
True

Error without exist_ok

Python 3.13
import os
os.makedirs('logs', exist_ok=True)
try:
    os.makedirs('logs')
except FileExistsError:
    print('Already exists')
Already exists

Details

Without exist_ok=True, running the program a second time fails with FileExistsError. For a single directory without parents, there is os.mkdir.

See also