ReadString

ReadString["file"]
reads the complete contents of a file and returns it as a string.

ReadString[stream]
reads everything from a stream and returns it as a string.

ReadString[proc]
reads everything generated by an external process and returns it as a string.

ReadString[src,term]
reads until the terminator term is encountered.

Details and OptionsDetails and Options

  • In ReadString[src,], src can have head InputStream or ProcessObject.
  • ReadString is a blocking function that by default will not return until it has read whatever it is specified to read.
  • With the option setting TimeConstraint->t, ReadString waits at most t seconds, then returns whatever it has read by that time.
  • The terminator term can be any of the following:
  • "string"literal string
    pattstring pattern
    EndOfBufferend of the buffer of available content
    EndOfFileend of the source of content (default)
  • ReadString[src] or ReadString[src,EndOfFile] reads until a stream has ended or the external process providing input has terminated.
  • If ReadString reaches the end of the specified source of content without finding the specified terminator, it will generate a message and return what it has read so far.
  • If ReadString is called on a source of content that has already been exhausted, it returns EndOfFile.
  • ReadString[src,EndOfBuffer] does not block and returns whatever is already available in the input buffer.

ExamplesExamplesopen allclose all

Basic Examples  (5)Basic Examples  (5)

Start the system shell process:

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

Write two commands into the system shell process:

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

Read the system shell output without blocking:

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

Start the system shell process:

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

Write two commands into the system shell process, then exit it:

In[2]:=
Click for copyable input

Return the full shell process output once it has finished running:

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

Start the system shell process:

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

Write some commands into the shell process:

In[2]:=
Click for copyable input

Read the shell process output right until the terminator is found:

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

Start the system shell process:

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

Write some commands into the shell process:

In[2]:=
Click for copyable input

Read the process output until a given string pattern is found:

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

Start the system shell process:

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

Write a command into the shell process:

In[2]:=
Click for copyable input

Read all partial output from the shell process:

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

Start the Wolfram Language from the shell:

In[4]:=
Click for copyable input

Read the Wolfram Language output:

In[5]:=
Click for copyable input
Out[5]=
Introduced in 2014
(10.0)