Filesystem

writefile

Writes Lua string bytes to a file inside the workspace, creating missing parent directories and replacing the file contents if the file already exists.

Syntax

writefile(path: string, content: string)
Aliases writefileasync

Arguments

NameTypeDescription
pathstringFile path resolved inside the workspace after separator normalization, workspace-boundary checks, and filename/extension checks. Absolute paths are accepted only if they resolve inside the workspace.
contentstringLua string bytes to write. The data is written in binary truncate mode; writefile does not add newlines, BOM bytes, null terminators, or other extra bytes.

Description

Writes Lua string bytes to a file inside the workspace, creating missing parent directories and replacing the file contents if the file already exists.

Call it with 2 parameter(s): path, content. The argument table explains which values are required and which ones only refine the behavior.

It does not return data. Treat the call as an action and handle errors around the call site when needed.

Example

Write bytes to a workspace file and replace any existing contents.

local path = "examples/config.json"

writefile(path, '{"enabled":true}')
print(readfile(path))

writefile(path, "")
print(#readfile(path))