Files and Paths
Use scoped file resources for new code, with Result keeping I/O failure visible.
Scoped Reads
file.open_read returns a scoped FileReader; with closes it on every exit path.
import:
file: IOError, open_read, read_text
func load(path: String) -> Result[String, IOError]:
with reader ?= open_read(path):
reader.read_text()
Scoped Writes
file.open_write creates or truncates a file and returns a scoped FileWriter.
import:
file: IOError, open_write, write_text
func save(path: String, text: String) -> Result[Void, IOError]:
with writer ?= open_write(path):
writer.write_text(text)
Append Mode
file.open_append creates the file if needed and preserves existing contents.
import:
file: IOError, open_append, write_text
func append_line(path: String, line: String) -> Result[Void, IOError]:
with writer ?= open_append(path):
writer.write_text(line + "\n")
Streaming Helpers
Use reader methods such as count_lines, read_chunk, chunks, and lines when whole-file materialization is not the right shape.
import:
file: IOError, count_lines, open_read
func count(path: String) -> Result[Int, IOError]:
with reader ?= open_read(path):
reader.count_lines()
path.join
path.join combines path segments without hand-written separators.
file: String = Path.join("data", "scores.txt")
Compatibility Helpers
system.read_file and system.write_file remain useful for simple eager whole-file programs.
match read_file("notes.txt"):
Ok(text): print(text)
Err(msg): print_error(msg)
Example
todo-add.brp
import:
file: IOError, open_append, write_text
path as Path
func add_todo(dir: String, item: String) -> Result[String, IOError]:
path: String = Path.join(dir, "todo.txt")
with writer ?= open_append(path):
ignored ?= writer.write_text(item + "\n")
Ok("saved")
func main(args: List[String]) -> Void:
match add_todo(".", "write docs"):
Ok(message): print(message) -- prints: saved
Err(err): print_error(err)
Try it
terminal
blorp run todo-add.brp