ReadLine

ReadLine["file"]

reads a line of text from a file and returns it as a string.

ReadLine[stream]

reads a line of text from a stream and returns it as a string.

ReadLine[proc]

reads a line of text generated by an external process and returns it as a string.

Details and Options

  • ReadLine reads until it encounters a newline, either "\n" or "\r\n".
  • ReadLine[File["file"],] is also supported.
  • ReadLine["file",] and ReadLine[File["file"],] will first open "file" for reading if it is not already open. The file will be left open until it is closed by a call to Close.
  • ReadLine is a blocking function that by default will not return until it has read a complete line of text.
  • With the option setting TimeConstraint->t, ReadLine waits at most t seconds, then returns whatever it has read by that time.
  • If ReadLine is called on a source of content that has already been exhausted, it returns EndOfFile.
  • ReadLine[src] is equivalent to ReadString[src,"\n"|"\r\n"].

Examples

open allclose all

Basic Examples  (3)

Read the first line of the file "ExampleData/strings":

Read the next line:

Both outputs are strings:

Close the file:

Start the system shell process:

Read all initial output; depending on the operating system and shell, this can result in an empty string:

Write a command into the shell:

Read the command output:

Start the system shell process:

Read all initial output; depending on the operating system and shell, this can result in an empty string:

Write two "echo" commands in the shell, then close it with "exit":

Read the shell process output:

Scope  (1)

Read the first line of a File object:

Close the file:

Introduced in 2014
 (10.0)
 |
Updated in 2016
 (11.0)