Python Glossary Русский

all Python 3osfunction

os.makedirs

os — directories

Recursively create a directory and any missing parents.

Syntax

os.makedirs(name, mode=0o777, exist_ok=False)

exist_ok=True suppresses the error only if a directory is already there: a regular file with the same name still raises FileExistsError. It is a safe replacement for the if not exists: makedirs race. The mode applies only to the leaf; intermediate directories get permissions from the umask. The documentation warns that '..' components in the part being created confuse the function.

Examples

import os
base = 'makedirs_demo'
d = os.path.join(base, 'a', 'b')
os.makedirs(d)
print(os.path.isdir(d))   # → True
os.removedirs(d)
os.makedirs('a/b/c', exist_ok=True)  # → создаёт вложенные папки
os.makedirs('new_dir', exist_ok=True)  # → не ошибка если папка уже есть

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy