Reading and Writing Wolfram System Files

Storing Wolfram Language Expressions in External Files

You can use files on your computer system to store definitions and results from the Wolfram Language. The most general approach is to store everything as plain text that is appropriate for input to the Wolfram Language. With this approach, a version of the Wolfram Language 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 (((x+y))^3) and outputs the result to a file called tmp:

Expand[(x+y)^3]>>tmp

Here are the contents of tmp. They can be used directly as input for the Wolfram Language:

FilePrint["tmp"]

This reads in tmp, evaluating the Wolfram Language input it contains:

<<tmp

This shows the contents of the file factors:

FilePrint["ExampleData/factors"]

This reads in the file and returns the last expression in it:

<<ExampleData/factors

If the Wolfram Language cannot find the file you ask it to read, it prints a message, then returns the symbol (RefLink[$Failed, paclet:ref/$Failed]):

<<faxors

When you read in a file with <<file, the Wolfram Language 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 (RefLink[Null, paclet:ref/Null]) after that expression.

If the Wolfram Language encounters a syntax error while reading a file, it reports the error, skips the remainder of the file, then returns (RefLink[$Failed, paclet:ref/$Failed]). If the syntax error occurs in the middle of a package that uses (RefLink[BeginPackage, paclet:ref/BeginPackage]) and other context manipulation functions, then the Wolfram Language tries to restore the context to what it was before the package was read.

Saving Multiple Wolfram Language Expressions

The Wolfram Language input files can contain any number of expressions. Each expression, however, must start on a new line. The expressions may continue for as many lines as necessary. Just as in a standard interactive Wolfram Language session, the expressions are processed as soon as they are complete. Note that in a file, unlike an interactive session, you can insert a blank line at any point without effect.

When you use expr>>>file, the Wolfram Language appends each new expression you give to the end of your file. If you use expr>>file, however, then the Wolfram Language instead wipes out anything that was in the file before, and then puts expr into the file.

This writes an expression to the file tmp:

Factor[x^6-1]>>tmp

Here are the contents of the file:

FilePrint["tmp"]

This appends another expression to the same file:

Factor[x^8-1]>>>tmp

Both expressions are now in the file:

FilePrint["tmp"]

If you are familiar with command-line operating systems, you will recognize the Wolfram Language redirection operators >>, >>>, and << as being analogous to the command-line operators >, >>, and <.

Saving Wolfram Language Expressions in Different Formats

When you use either >> or >>> to write expressions to files, the expressions are usually given in Wolfram Language input format, so that you can read them back into the Wolfram Language. Sometimes, however, you may want to save expressions in other formats. You can do this by explicitly wrapping a format directive such as (RefLink[OutputForm, paclet:ref/OutputForm]) around the expression you write out.

This writes an expression to the file tmp in output format:

OutputForm[Factor[x^6-1]]>>tmp

The expression in tmp is now in output format:

FilePrint["tmp"]

Saving Definitions of Wolfram Language Objects

One of the most common reasons for using files is to save definitions of Wolfram Language objects, to be able to read them in again in a subsequent Wolfram Language session. The operators >> and >>> allow you to save Wolfram Language expressions in files. You can use the function (RefLink[Save, paclet:ref/Save]) to save complete definitions of Wolfram Language objects in a form suitable for execution in subsequent Wolfram Language sessions.

Saving definitions in plain text files.

This assigns a value to the symbol a:

a=2-x^2

You can use (RefLink[Save, paclet:ref/Save]) to write the definition of a to a file:

Save["afile",a]

Here is the definition of a that was saved in the file:

FilePrint["afile"]

This defines a function f that depends on the symbol a previously defined:

f[z_]:=a^2-2

This saves the complete definition of f in a file:

Save["ffile",f]

The file contains not only the definition of f itself, but also the definition of the symbol a on which f depends:

FilePrint["ffile"]

This clears the definitions of f and a:

Clear[f,a]

You can reinstate the definitions you saved simply by reading in the file ffile:

<<ffile

The function (RefLink[Save, paclet:ref/Save]) makes use of the output forms (RefLink[Definition, paclet:ref/Definition]) and (RefLink[FullDefinition, paclet:ref/FullDefinition]), which print as definitions of Wolfram Language symbols. In some cases, you may find it convenient to use these output forms directly.

The output form (RefLink[Definition, paclet:ref/Definition])[f] prints as the sequence of definitions that have been made for f:

Definition[f]

(RefLink[FullDefinition, paclet:ref/FullDefinition])[f] includes definitions of the objects on which f depends:

FullDefinition[f]

When you define a new object in the Wolfram Language, 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 Wolfram Language 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 (RefLink[Save, paclet:ref/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, (RefLink[Save, paclet:ref/Save]) never includes definitions for symbols that have the attribute (RefLink[Protected, paclet:ref/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 (RefLink[Save, paclet:ref/Save]) back into a new Wolfram Language session will set up the definitions of your objects exactly as you had them before.

Saving Wolfram Language Definitions in Encoded Form

When you create files for input to the Wolfram Language, 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 the Wolfram Language. You can create encoded files using the Wolfram Language function (RefLink[Encode, paclet:ref/Encode]).

Creating and reading encoded files.

This writes an expression in plain text to the file tmp:

Factor[x^2-1]>>tmp

This writes an encoded version of the file tmp to the file tmp.x:

Encode["tmp","tmp.x"]

Here are the contents of the encoded file. The only recognizable part is the special Wolfram Language comment at the beginning:

FilePrint["tmp.x"]

Even though the file is encoded, you can still read it into the Wolfram Language using the << operator:

<<tmp.x

Saving definitions in internal Wolfram System 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 Wolfram System format, rather than as text. You can do this using (RefLink[DumpSave, paclet:ref/DumpSave]).

This saves the definition for f in internal Wolfram System format:

DumpSave["ffile.mx",f]

You can still use << to read the definition in:

<<ffile.mx

<< recognizes when a file contains definitions in internal Wolfram System format, and operates accordingly. One subtlety is that the internal Wolfram System 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 (RefLink[DumpSave, paclet:ref/DumpSave])["package`",…] then the Wolfram Language 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:

DumpSave["gffile`",f]

<< automatically picks out the file with the appropriate name for your computer system:

<<gffile`