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 OptionsDetails and Options

  • ReadLine reads until it encounters a newline, either "\n" or "\r\n".
  • ReadLine[File["file"],] is also supported.
  • ReadLine["file",] and ReadSLine[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"|"\n\n"].

ExamplesExamplesopen allclose all

Basic Examples  (3)Basic Examples  (3)

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

In[1]:=
Click for copyable input
Out[1]=

Read the next line:

In[2]:=
Click for copyable input
Out[2]=

Both outputs are strings:

In[3]:=
Click for copyable input
Out[3]=

Close the file:

In[4]:=
Click for copyable input
Out[4]=

Start the system shell process:

In[1]:=
Click for copyable input
Out[1]=

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

In[2]:=
Click for copyable input

Write a command into the shell:

In[3]:=
Click for copyable input

Read the command output:

In[4]:=
Click for copyable input
Out[4]=

Start the system shell process:

In[1]:=
Click for copyable input
Out[1]=

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

In[2]:=
Click for copyable input

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

In[3]:=
Click for copyable input

Read the shell process output:

In[4]:=
Click for copyable input
Out[4]=
In[5]:=
Click for copyable input
Out[5]=
In[6]:=
Click for copyable input
Out[6]=
Introduced in 2014
(10.0)
| Updated in 2016
(11.0)