write_yaml()

Write a Python value to YAML at path or stdout.

Usage

write_yaml(
    value,
    path=None,
    multi=False,
    append=False,
    width=80,
)

Parameters

value: object

Python value or Yaml to serialize; for multi the value must be a sequence of documents.

path: str | os.PathLike | text file-like | None = None

Destination path or object with .write(str); when None the YAML is written to stdout. Filesystem paths beginning with ~ are expanded using os.path.expanduser().

multi: bool = False

Emit a multi-document stream when true; otherwise a single document.

append: bool = False

Append complete YAML documents to a filesystem path instead of replacing it.

width: int | None = 80
Target maximum line width. None disables wrapping.

Returns

None
None

Raises

IOError

When writing to the file or stdout fails.

TypeError

When argument types are unsupported, or multi is true and value is not a sequence.

ValueError
When width is less than one.

Examples

    >>> write_yaml({'foo': 1}, path='out.yml')
    >>> Path('out.yml').exists()
True
    >>> write_yaml(['first', 'second'], multi=True)  # prints two documents beginning with '---'