Construct a File Path Programmatically
Construct relative and absolute file paths.
To a Relative Location...
In many situations, a relative file path is more robust than an absolute one. If a notebook is bundled with data or other files, a relative file path continues to work when the entire bundle is moved to a new location.
Without arguments, NotebookDirectory gives the path to the notebook that contains the evaluation:
Specify a path relative to this notebook’s directory with FileNameJoin:
Specify a path relative to this notebook’s directory’s parent directory using ParentDirectory:
- Use the FileNameJoin expression directly in your code, not the result of evaluating it. The result of evaluating FileNameJoin is an absolute file path.
To an Absolute Location...
You can use FileNameJoin and similar path construction functions to compute absolute file paths. Use the output of path expressions to embed absolute file paths in your code:
- Many useful file paths are predefined, for example, $HomeDirectory and $UserDocumentsDirectory.