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

Reading and Writing Mathematica FilesImporting and Exporting Data

1.11.2 Advanced Topic: Finding and Manipulating Files

Although the details of how files are named and organized differ from one computer system to another, Mathematica provides some fairly general mechanisms for finding and handling files.

Mathematica assumes that files on your computer system are organized in a collection of directories. At any point, you have a current working directory. You can always refer to files in this directory just by giving their names.

Functions for finding and manipulating files.

This is the current working directory. The form it has differs from one computer system to another.

In[1]:= Directory[ ]

Out[1]= /users/sw

This resets the current working directory.

In[2]:= SetDirectory["Examples"]

Out[2]= /users/sw/Examples

This gives a list of all files in your current working directory whose names match the form Test*.m.

In[3]:= FileNames["Test*.m"]

Out[3]= {Test1.m, Test2.m, TestFinal.m}

Although you usually want to create files only in your current working directory, you often need to read in files from other directories. As a result, when you ask Mathematica to read in a file with a particular name, Mathematica automatically searches a list of directories (specified by the value of the search path variable $Path) to try and find a file with that name.

One issue in handling files in Mathematica is that the form of file and directory names varies between computer systems. This means for example that names of files which contain standard Mathematica packages may be quite different on different systems. Through a sequence of conventions, it is however possible to read in a standard Mathematica package with the same command on all systems. The way this works is that each package defines a so-called Mathematica context, of the form name`name`. On each system, all files are named in correspondence with the contexts they define. Then when you use the command <<name`name` Mathematica automatically translates the context name into the file name appropriate for your particular computer system.

Searching for text in files.

This searches for all lines in the file BookIndex containing diagrams.

In[4]:= FindList["BookIndex", "diagrams"]


Reading and Writing Mathematica FilesImporting and Exporting Data