Filesystem

readfile

Reads an existing regular file inside the workspace in binary mode and returns its bytes as a Lua string.

Syntax

readfile(path: string): string
Aliases readfileasync

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.

Returns

NameTypeDescription
contentstringFile contents returned as a Lua string with an explicit length. Bytes such as null bytes, line endings, and BOM bytes are not transformed by readfile.

Description

Reads an existing regular file inside the workspace in binary mode and returns its bytes as a Lua string.

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

It returns content (string). Use the returns table to separate successful values from nil results and recoverable errors.

Example

Read bytes from an existing workspace file.

local path = "examples/config.json"
writefile(path, '{"enabled":true}')

local content = readfile(path)
print(content)
print(#content)