On this page
os.makedirs
Prev: os.lstat | Up: os | Next: os.mkdir
Rel: os.abort, os.access, os.chdir, os.chmod
Ref: docs.python.org — os.makedirs
Tags: #public #python3 #stdlib #os
Import
import os
from os import makedirs
Signature & Overview
os.makedirs(name, mode=511, exist_ok=False)
Super-mkdir; create a leaf directory and all intermediate ones. Works like mkdir, except that any intermediate path segment (not just the rightmost) will be created if it does not exist. If the target
Usage
import os
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
nested = Path(tmp) / "a" / "b" / "c"
os.makedirs(nested, exist_ok=True)
assert nested.is_dir()
Gotchas
- Pass
exist_ok=Trueto avoidFileExistsError: Equivalent toPath(p).mkdir(parents=True, exist_ok=True).
REPL Inspect
from os import makedirs
help(makedirs)
Linked from
Related notes
- tuts/python3/stdlib_module/os/lstat Linked here; links to this page; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/mkdir Linked here; links to this page; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/os/abort Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/access Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chdir Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chmod Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chown shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/close shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/cpu_count shared tags: #os, #stdlib