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

Documentation / Mathematica / The Mathematica Book / A Practical Introduction to Mathematica / Files and External Operations /

1.11.1 Reading and Writing Mathematica Files

You can use files on your computer system to store definitions and results from Mathematica. The most general approach is to store everything as plain text that is appropriate for input to Mathematica. With this approach, a version of Mathematica running on one computer system produces files that can be read by a version running on any computer system. In addition, such files can be manipulated by other standard programs, such as text editors.

Reading and writing files.

This expands , and outputs the result to a file called tmp.

In[1]:= Expand[ (x + y)^3 ] >> tmp

Here are the contents of tmp. They can be used directly as input for Mathematica.

In[2]:= !!tmp

"x^3 + 3*x^2*y + 3*x*y^2 + y^3"

This reads in tmp, evaluating the Mathematica input it contains.

In[3]:= <<tmp

Out[3]=

If you are familiar with Unix or MS-DOS operating systems, you will recognize the Mathematica redirection operators >>, >>> and << as being analogous to the shell operators >, >> and <.

The redirection operators >> and >>> are convenient for storing results you get from Mathematica. The function Save["name", f, g, ... ] allows you to save definitions for variables and functions.

Saving definitions in plain text files.

Here is a definition for a function f.

In[4]:= f[x_] := x^2 + c

This gives c the value 17.

In[5]:= c = 17

Out[5]=

This saves the definition of f in the file ftmp.

In[6]:= Save["ftmp", f]

Mathematica automatically saves both the actual definition of f, and the definition of c on which it depends.

In[7]:= !!ftmp

"f[x_] := x^2 + c



c = 17"

This clears the definitions of f and c.

In[8]:= Clear[f, c]

You can reinstate the definitions you saved simply by reading in the file ftmp.

In[9]:= <<ftmp

Out[9]=

Typical names of Mathematica files.

If you use a notebook interface to Mathematica, then the Mathematica front end allows you to save complete notebooks, including not only Mathematica input and output, but also text, graphics and other material.

It is conventional to give Mathematica notebook files names that end in .nb, and most versions of Mathematica enforce this convention.

When you open a notebook in the Mathematica front end, Mathematica will immediately display the contents of the notebook, but it will not normally send any of these contents to the kernel for evaluation until you explicitly request this to be done.

Within a Mathematica notebook, however, you can use the Cell menu in the front end to identify certain cells as initialization cells, and if you do this, then the contents of these cells will automatically be evaluated whenever you open the notebook.

The I in the cell bracket indicates that the second cell is an initialization cell that will be evaluated whenever the notebook is opened.

It is sometimes convenient to maintain Mathematica material both in a notebook which contains explanatory text, and in a package which contains only raw Mathematica definitions. You can do this by putting the Mathematica definitions into initialization cells in the notebook. Every time you save the notebook, the front end will then allow you to save an associated .m file which contains only the raw Mathematica definitions.