How to | Locate and Use Files
Mathematica offers support for a large number of file formats for importing, exporting, or other operations. It has several standard locations where it looks for files. You can change these locations to place other directories in
Mathematica's default search path.
Mathematica is able to use files without the full file path specified. However, the files must be located in one of the directories that
Mathematica searches for files.
By default,
Mathematica is able to use files in the directories that appear in
$Path:
| Out[1]= |  |
Mathematica is also able to use files located in your current working directory without a full file path.
View your current working directory by evaluating
Directory
:
| Out[2]= |  |
You can see a list of all files in the current working directory by evaluating
FileNames
:
| Out[3]= |  |
You can view the file names you want by using wildcards:
| Out[4]= |  |
There are a number of methods you can use to access a file if it is in a directory that is not on
$Path or in your current working directory.
For example, you can specify the full path of a file or folder that you want to use. See
How to: Insert a File Path to view the different methods available for doing this.
Specify the full file path:
| Out[1]= |  |
Or, use
SetDirectory to change your current working directory:
| Out[2]= |  |
| Out[3]= |  |
To use a file that is in one of the directories on
$Path or your current working directory, you only need to specify the name of the file.
Import the file

from the current working directory:
| Out[4]= |  |
Use
ResetDirectory
to reset the current working directory to its previous value:
| Out[5]= |  |
You can also append a directory to
$Path.
Use
AppendTo with
$Path and the directory path:
| Out[6]= |  |
You can now import

without changing your current working directory:
| Out[7]= |  |
| Out[8]= |  |
Note that the change to
$Path is temporary, and the value is reset when you restart
Mathematica or the kernel.
To make the change permanent, you can add the previous
AppendTo command to one of the following two
init.m files. Here,
FileNameJoin is used to construct a full file path from
$UserBaseDirectory and
$BaseDirectory:
| Out[1]= |  |
| Out[2]= |  |
Just like
$Path,
$UserBaseDirectory, and
$BaseDirectory, other system variables can be used to construct file paths. Some commonly used system variables that represent directories are
$RootDirectory,
$HomeDirectory, and
$InstallationDirectory.