Create directory

2 snippets across 2 stacks — Bash & Linux, Python

SHBash & Linux

Create Directories

SH · Navigation & Files
syntax
mkdir [options] directory...
example
mkdir -p src/components/auth
mkdir -m 750 secrets

Note -p creates the full path including any missing parent directories and will not error if the directory already exists. -m sets permissions at creation time.

PYPython

pathlib (Modern Path Handling)

PY · File I/O
syntax
from pathlib import Path
example
from pathlib import Path

data_dir = Path("project") / "data"
data_dir.mkdir(parents=True, exist_ok=True)

config = data_dir / "settings.json"
config.write_text('{"debug": true}', encoding="utf-8")
print(config.read_text())
print(config.suffix, config.stem)
output
{"debug": true}
.json settings

Note pathlib is the modern replacement for os.path. The / operator joins paths. Use .resolve() for absolute paths, .exists() to check existence.