Skip to content

Latest commit

 

History

History
51 lines (40 loc) · 1022 Bytes

File metadata and controls

51 lines (40 loc) · 1022 Bytes

write_yaml

write_yaml() serializes Python data to YAML and writes it to disk.

Signature

write_yaml(
    file_path: str | Path,
    data: Any,
    create_if_missing: bool = True,
    allow_unicode: bool = True,
    sort_keys: bool = False,
    on_error: Literal["raise", "log", "silent"] | None = None,
) -> None

Example: write a readable YAML config

from clevertools import write_yaml

write_yaml(
    "config.yaml",
    {
        "service": "clevertools",
        "enabled": True,
        "labels": ["yaml", "config"],
    },
    allow_unicode=True,
    sort_keys=False,
)

Example: preserve non-ASCII text

from clevertools import write_yaml

write_yaml(
    "content/meta.yaml",
    {"title": "Überblick", "language": "de"},
    allow_unicode=True,
)

Notes

  • data must not be None.
  • With create_if_missing=True, parent folders are created automatically.
  • sort_keys=False is useful when output order should stay close to your input structure.