Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Reading lines

readline() keeps the terminating newline, translating LF, CRLF, and bare CR to "\n". An empty line is "\n"; EOF is "". The last line need not have a terminating newline. Unlike input(), line reading retains that terminator.

Run this in a scratch directory: it replaces plenty-lines.txt.

def count_lines() -> Result[i64, IoError | OverflowError[i64]]:
    write_text("plenty-lines.txt", "first\r\n\rfinal")?
    mut count = 0
    with open("plenty-lines.txt")? as file:
        while True:
            line = file.readline()?
            if line == "":
                break
            count = count + 1
    Ok(count)

def main() -> Result[(), AllocError | IoError | OverflowError[i64]]:
    print(count_lines()?)?
    Ok(())
3

readline() and read() share newline state. Their errors may consume input; retrying is not a rollback. Direct for line in file iteration is unsupported. For size-limited reads and saved positions, see the text I/O reference.