write() writes plain text or raw bytes to a file.
write(
file_path: str | Path,
data: str | bytes,
create_if_missing: bool = True,
on_error: Literal["raise", "log", "silent"] | None = None,
) -> Nonestrdata is written as UTF-8 textbytesdata is written unchanged- parent folders are created automatically when
create_if_missing=True
from clevertools import write
write("notes.txt", "Hello from clevertools")from clevertools import write
write("cache/blob.bin", b"\x00\x01hello\xff")from clevertools import write
write("existing/output.txt", "updated content", create_if_missing=False, on_error="raise")- With
create_if_missing=False, the file must already exist and must be a normal file. - Non-
strand non-bytespayloads are rejected. - The function does not return the written content; it either succeeds or follows the shared error policy.