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
Related
larzfsRead/write/ls/copy/basename/dirname→Build a CLI Tool with Subcommands in LarzscriptBuild a real command-line tool with named subcommands and auto-generated help, the shape every larzscript-* showcase app in this ecosystem uses.→Parse Command-Line Arguments in LarzscriptParse --flag=value pairs and positional arguments out of argv, without a full CLI framework.→Pretty-Print a Table in LarzscriptRender tabular data as an aligned ASCII table for the terminal, or Markdown for a README.→