io.lines()

Type Function
Library io.*
Return value Function (iterator)
Revision Release 2024.3703
Keywords io, lines, file
See also io.input()

Overview

Opens the given file name in read mode and returns an iterator function that, each time it is called, returns a new line from the file.

The function will open the file and iterate over all lines of the file. When the iterator function detects the end of file, it returns nil (to finish the loop) and automatically closes the file.

The call io.lines() with no file is similar to io.input():lines(); that is, it iterates over the lines of the default input file, except it does not close the file when the loop ends.

This function returns an iterator function that, each time it is called, returns a new line from the file.

Syntax

io.lines( file )
file (required)

Object. File to read. You must use system.pathForFile() to create a file name and path to the system.ResourceDirectory, system.DocumentsDirectory, system.ApplicationSupportDirectory, system.TemporaryDirectory, or system.CachesDirectory constants.

Example

local path = system.pathForFile( "data.txt", system.DocumentsDirectory )

for line in io.lines( path ) do
    print( line )
end