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

Reading and Writing Mathematica Files

Particularly if you use a text-based Mathematica interface, you will often need to read and write files containing definitions and results from Mathematica. "Reading and Writing Mathematica Files: Basics" gives a general discussion of how to do this. Here are some more details.
<<file or Get["file"]read in a file of Mathematica input, and return the last expression in the file
FilePrint["file"]display the contents of a file

Reading files.

This shows the contents of the file factors.
This reads in the file, and returns the last expression in it.
In[2]:=
Click for copyable input
Out[2]=
If Mathematica cannot find the file you ask it to read, it prints a message, then returns the symbol $Failed.
In[3]:=
Click for copyable input
Out[3]=
Mathematica input files can contain any number of expressions. Each expression, however, must start on a new line. The expressions may however continue for as many lines as necessary. Just as in a standard interactive Mathematica session, the expressions are processed as soon as they are complete. Note, however, that in a file, unlike an interactive session, you can insert a blank line at any point without effect.
When you read in a file with <<file, Mathematica returns the last expression it evaluates in the file. You can avoid getting any visible result from reading a file by ending the last expression in the file with a semicolon, or by explicitly adding Null after that expression.
If Mathematica encounters a syntax error while reading a file, it reports the error, skips the remainder of the file, then returns $Failed. If the syntax error occurs in the middle of a package which uses BeginPackage and other context manipulation functions, then Mathematica tries to restore the context to what it was before the package was read.
expr>>file or Put[expr,"file"]write an expression to a file
expr>>>file or PutAppend[expr,"file"]
append an expression to a file

Writing expressions to files.

This writes an expression to the file tmp.
In[4]:=
Click for copyable input
Here are the contents of the file.
This appends another expression to the same file.
In[6]:=
Click for copyable input
Both expressions are now in the file.
When you use expr>>>file, Mathematica appends each new expression you give to the end of your file. If you use expr>>file, however, then Mathematica instead wipes out anything that was in the file before, and then puts expr into the file.
When you use either >> or >>> to write expressions to files, the expressions are usually given in Mathematica input format, so that you can read them back into Mathematica. Sometimes, however, you may want to save expressions in other formats. You can do this by explicitly wrapping a format directive such as OutputForm around the expression you write out.
This writes an expression to the file tmp in output format.
In[8]:=
Click for copyable input
The expression in tmp is now in output format.
One of the most common reasons for using files is to save definitions of Mathematica objects, to be able to read them in again in a subsequent Mathematica session. The operators >> and >>> allow you to save Mathematica expressions in files. You can use the function Save to save complete definitions of Mathematica objects, in a form suitable for execution in subsequent Mathematica sessions.
Save["file",symbol]save the complete definitions for a symbol in a file
Save["file","form"]save definitions for symbols whose names match the string pattern form
Save["file","context`"]save definitions for all symbols in the specified context
Save["file",{object1,object2,...}]save definitions for several objects

Writing out definitions.

This assigns a value to the symbol a.
In[10]:=
Click for copyable input
Out[10]=
You can use Save to write the definition of a to a file.
In[11]:=
Click for copyable input
Here is the definition of a that was saved in the file.
When you define a new object in Mathematica, your definition will often depend on other objects that you defined before. If you are going to be able to reconstruct the definition of your new object in a subsequent Mathematica session, it is important that you store not only its own definition, but also the definitions of other objects on which it depends. The function Save looks through the definitions of the objects you ask it to save, and automatically also saves all definitions of other objects on which it can see that these depend. However, in order to avoid saving a large amount of unnecessary material, Save never includes definitions for symbols that have the attribute Protected. It assumes that the definitions for these symbols are also built in. Nevertheless, with such definitions taken care of, it should always be the case that reading the output generated by Save back into a new Mathematica session will set up the definitions of your objects exactly as you had them before.
This defines a function f which depends on the symbol a defined above.
In[13]:=
Click for copyable input
This saves the complete definition of f in a file.
In[14]:=
Click for copyable input
The file contains not only the definition of f itself, but also the definition of the symbol a on which f depends.
The function Save makes use of the output forms Definition and FullDefinition, which print as definitions of Mathematica symbols. In some cases, you may find it convenient to use these output forms directly.
The output form Definition[f] prints as the sequence of definitions that have been made for f.
In[16]:=
Click for copyable input
Out[16]=
FullDefinition[f] includes definitions of the objects on which f depends.
In[17]:=
Click for copyable input
Out[17]=
When you create files for input to Mathematica, you usually want them to contain only "plain text", which can be read or modified directly. Sometimes, however, you may want the contents of a file to be "encoded" so that they cannot be read or modified directly as plain text, but can be loaded into Mathematica. You can create encoded files using the Mathematica function Encode.
Encode["source","dest"]write an encoded version of the file source to the file dest
<<destread in an encoded file
Encode["source","dest","key"]encode with the specified key
Get["dest","key"]read in a file that was encoded with a key
Encode["source","dest",MachineID->"ID"]
create an encoded file which can only be read on a machine with a particular ID

Creating and reading encoded files.

This writes an expression in plain text to the file tmp.
In[18]:=
Click for copyable input
This writes an encoded version of the file tmp to the file tmp.x.
In[19]:=
Click for copyable input
Here are the contents of the encoded file. The only recognizable part is the special Mathematica comment at the beginning.
Even though the file is encoded, you can still read it into Mathematica using the << operator.
In[21]:=
Click for copyable input
Out[21]=
DumpSave["file.mx",symbol]save definitions for a symbol in internal Mathematica format
DumpSave["file.mx","context`"]save definitions for all symbols in a context
DumpSave["file.mx",{object1,object2,...}]
save definitions for several symbols or contexts
DumpSave["package`",objects]save definitions in a file with a specially chosen name

Saving definitions in internal Mathematica format.

If you have to read in very large or complicated definitions, you will often find it more efficient to store these definitions in internal Mathematica format, rather than as text. You can do this using DumpSave.
This saves the definition for f in internal Mathematica format.
In[22]:=
Click for copyable input
Out[22]=
You can still use << to read the definition in.
In[23]:=
Click for copyable input
<< recognizes when a file contains definitions in internal Mathematica format, and operates accordingly. One subtlety is that the internal Mathematica format differs from one computer system to another. As a result, .mx files created on one computer cannot typically be read on another.
If you use DumpSave["package`", ...] then Mathematica will write out definitions to a file with a name like package.mx/system/package.mx, where system identifies your type of computer system.
This creates a file with a name that reflects the name of the computer system being used.
In[24]:=
Click for copyable input
Out[24]=
<< automatically picks out the file with the appropriate name for your computer system.
In[25]:=
Click for copyable input