Skip to content

pyio.write_lines

Daniel Flassig edited this page Jul 7, 2026 · 2 revisions

Writes a table of strings to a text file, one entry per line.

pyio.write_lines(file, lines [, encoding])
Parameter Type Description
file path handle The file to write. The path handle must grant write access.
lines { (string)* } A table of strings, each entry represents one line
encoding string (optional) Encoding of the output file ("utf8", "utf8-bom", "utf16le", "utf16be", "ascii")

Return value

Type Description
boolean true on success, false if the file could not be written (see remarks)

Example:

Writing the following Lua table

local lines = {
    "# Sample file",
    "Hello World",
    "",
    "Pi = 3.14"
}

local folder = pyui.select_folder("full")
if folder then
    pyio.write_lines(folder:append_path("sample.txt"), lines)
end

will create a text file with the following content:

# Sample file
Hello World

Pi = 3.14

Remarks:

Each entry in the table is written as a separate line in the file. A newline is automatically appended after every line.

If a table entry is nil or cannot be converted to a string, it is treated as an empty line.

Supported encodings include UTF-8 (with or without BOM), UTF-16 (little endian and big endian), and ASCII. If no encoding is specified, UTF-8 is used by default.

For UTF-16 encodings, Windows-style line endings (CR+LF) are written. For UTF-8 and ASCII, line endings are written as LF.

A writable path handle can be obtained from pyui.select_folder with "full" access. Passing a read-only handle raises an error. false is returned when the file cannot be opened or written.

Version Support:

Minimum PYTHA Version: V26

See also:

pyio, pyio.parse_lines, pyui.select_folder

Clone this wiki locally