CLI & text · real, tested code

Read a File Line by Line in Larzscript

Read a text file's contents and split it into lines - the everyday first step of any script that processes a file.

fs wraps the core read_file()/write_file() builtins with the directory helpers (exists/mkdirs/basename/dirname) real scripts need around them.

larzscript pkg install fs

Code

import "fs" as fs

write_file("/tmp/lz_cookbook_demo.txt", "first line\nsecond line\nthird line")
let text = read_file("/tmp/lz_cookbook_demo.txt")
for line in text.split("\n") {
  print("-", line)
}
print("exists:", fs.exists("/tmp/lz_cookbook_demo.txt"))
Output
- first line
- second line
- third line
exists: true