How to | Locate and Use Files

The Wolfram Language 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 the Wolfram Language's default search path.

The Wolfram Language is able to use files without the full file path specified. However, the files must be located in one of the directories that the Wolfram Language searches for files.

By default, the Wolfram Language is able to use files in the directories that appear in $Path:

In[1]:=
Click for copyable input
Out[1]=

The Wolfram Language 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[]:

In[2]:=
Click for copyable input
Out[2]=

You can see a list of all files in the current working directory by evaluating FileNames[]:

In[3]:=
Click for copyable input
Out[3]=

You can view the file names you want by using wildcards:

In[4]:=
Click for copyable input
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:

In[5]:=
Click for copyable input
Out[5]=

Or, use SetDirectory to change your current working directory:

In[6]:=
Click for copyable input
Out[6]=
In[7]:=
Click for copyable input
Out[7]=

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:

In[8]:=
Click for copyable input
Out[8]=

Use ResetDirectory[] to reset the current working directory to its previous value:

In[9]:=
Click for copyable input
Out[9]=

You can also append a directory to $Path.

Use AppendTo with $Path and the directory path:

In[10]:=
Click for copyable input
Out[10]=

You can now import without changing your current working directory:

In[11]:=
Click for copyable input
Out[11]=
In[12]:=
Click for copyable input
Out[12]=

Note that the change to $Path is temporary, and the value is reset when you restart the Wolfram System 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:

In[13]:=
Click for copyable input
Out[13]=
In[14]:=
Click for copyable input
Out[14]=

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.