This is documentation for Mathematica 6, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Reading Textual Data

With <<, you can read files which contain Mathematica expressions given in input form. Sometimes, however, you may instead need to read files of data in other formats. For example, you may have data generated by an external program which consists of a sequence of numbers separated by spaces. This data cannot be read directly as Mathematica input. However, the function ReadList can take such data from a file or input stream, and convert it to a Mathematica list.
ReadList["file",Number]read a sequence of numbers from a file, and put them in a Mathematica list

Reading numbers from a file.

Here is a file of numbers.
This reads all the numbers in the file, and returns a list of them.
Click for copyable input
ReadList["file",{Number,Number}]read numbers from a file, putting each successive pair into a separate list
put each successive block of n numbers in a separate list
put all the numbers on each line of the file into a separate list

Reading blocks of numbers.

This puts each successive pair of numbers from the file into a separate list.
Click for copyable input
This makes each line in the file into a separate list.
Click for copyable input
ReadList can handle numbers which are given in Fortran-like "E" notation. Thus, for example, ReadList will read 2.5E+5 as 2.5105. Note that ReadList can handle numbers with any number of digits of precision.
Here is a file containing numbers in Fortran-like "E" notation.
ReadList can handle numbers in this form.
Click for copyable input
ReadList["file",type]read a sequence of objects of a particular type
ReadList["file",type,n]read at most n objects

Reading objects of various types.

ReadList can read not only numbers, but also a variety of other types of object. Each type of object is specified by a symbol such as Number.
Here is a file containing text.
This produces a list of the characters in the file, each given as a one-character string.
Click for copyable input
Here are the integer codes corresponding to each of the bytes in the file.
Click for copyable input
This puts the data from each line in the file into a separate list.
Click for copyable input
Bytesingle byte of data, returned as an integer
Charactersingle character, returned as a one-character string
Realapproximate number in Fortran-like notation
Numberexact or approximate number in Fortran-like notation
Wordsequence of characters delimited by word separators
Recordsequence of characters delimited by record separators
Stringstring terminated by a newline
Expressioncomplete Mathematica expression
Hold[Expression]complete Mathematica expression, returned inside Hold

Types of objects to read.

This returns a list of the "words" in the file strings.
Click for copyable input
ReadList allows you to read "words" from a file. It considers a "word" to be any sequence of characters delimited by word separators. You can set the option WordSeparators to specify the strings you want to treat as word separators. The default is to include spaces and tabs, but not to include, for example, standard punctuation characters. Note that in all cases successive words can be separated by any number of word separators. These separators are never taken to be part of the actual words returned by ReadList.
option name
default value
RecordListsFalsewhether to make a separate list for the objects in each record
RecordSeparators{"\r\n", "\n","\r"}separators for records
WordSeparators{" ","\t"}separators for words
NullRecordsFalsewhether to keep zero-length records
NullWordsFalsewhether to keep zero-length words
TokenWords{}words to take as tokens

Options for ReadList.

This reads the text in the file strings as a sequence of words, using the letter e and . as word separators.
Click for copyable input
Mathematica considers any data file to consist of a sequence of records. By default, each line is considered to be a separate record. In general, you can set the option RecordSeparators to give a list of separators for records. Note that words can never cross record separators. As with word separators, any number of record separators can exist between successive records, and these separators are not considered to be part of the records themselves.
By default, each line of the file is considered to be a record.
Click for copyable input
Here is a file containing three "sentences" ending with periods.
This allows both periods and newlines as record separators.
Click for copyable input
This puts the words in each "sentence" into a separate list.
Click for copyable input
read the whole of a file as a single string
make a list of those parts of a file which lie between the lsepi and the rsepi

Settings for the RecordSeparators option.

Here is a file containing some text.
This reads all the text in the file source, and returns it as a single string.
Click for copyable input
This gives a list of the parts of the file that lie between (: and :) separators.
Click for copyable input
By choosing appropriate separators, you can pick out specific parts of files.
Click for copyable input
Mathematica usually allows any number of appropriate separators to appear between successive records or words. Sometimes, however, when several separators are present, you may want to assume that a "null record" or "null word" appears between each pair of adjacent separators. You can do this by setting the options NullRecords->True or NullWords->True.
Here is a file containing "words" separated by colons.
Here the repeated colons are treated as single separators.
Click for copyable input
Now repeated colons are taken to have null words in between.
Click for copyable input
In most cases, you want words to be delimited by separators which are not themselves considered as words. Sometimes, however, it is convenient to allow words to be delimited by special "token words", which are themselves words. You can give a list of such token words as a setting for the option TokenWords.
Here is some text.
This reads the text, using the specified token words to delimit words in the text.
Click for copyable input
You can use ReadList to read Mathematica expressions from files. In general, each expression must end with a newline, although a single expression may go on for several lines.
Here is a file containing text that can be used as Mathematica input.
This reads the text in exprs as Mathematica expressions.
Click for copyable input
This prevents the expressions from being evaluated.
Click for copyable input
ReadList can insert the objects it reads into any Mathematica expression. The second argument to ReadList can consist of any expression containing symbols such as Number and Word specifying objects to read. Thus, for example, ReadList["file", {Number, Number}] inserts successive pairs of numbers that it reads into lists. Similarly, ReadList["file", Hold[Expression]] puts expressions that it reads inside Hold.
If ReadList reaches the end of your file before it has finished reading a particular set of objects you have asked for, then it inserts the special symbol EndOfFile in place of the objects it has not yet read.
Here is a file of numbers.
The symbol EndOfFile appears in place of numbers that were needed after the end of the file was reached.
Click for copyable input
ReadList["!command",type]execute a command, and read its output
ReadList[stream,type]read any input stream

Reading from commands and streams.

This executes the Unix command date, and reads its output as a string.
Click for copyable input
OpenRead["file"]open a file for reading
OpenRead["!command"]open a pipe for reading
Read[stream,type]read an object of the specified type from a stream
Skip[stream,type]skip over an object of the specified type in an input stream
Skip[stream,type,n]skip over n objects of the specified type in an input stream
Close[stream]close an input stream

Functions for reading from input streams.

ReadList allows you to read all the data in a particular file or input stream. Sometimes, however, you want to get data a piece at a time, perhaps doing tests to find out what kind of data to expect next.
When you read individual pieces of data from a file, Mathematica always remembers the "current point" that you are at in the file. When you call OpenRead, Mathematica sets up an input stream from a file, and makes your current point the beginning of the file. Every time you read an object from the file using Read, Mathematica sets your current point to be just after the object you have read. Using Skip, you can advance the current point past a sequence of objects without actually reading the objects.
Here is a file of numbers.
This opens an input stream from the file.
Click for copyable input
This reads the first number from the file.
Click for copyable input
This reads the second pair of numbers.
Click for copyable input
This skips the next number.
Click for copyable input
And this reads the remaining numbers.
Click for copyable input
This closes the input stream.
Click for copyable input
You can use the options WordSeparators and RecordSeparators in Read and Skip just as you do in ReadList.
Note that if you try to read past the end of file, Read returns the symbol EndOfFile.