Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I cannot find how to use relative paths in Mathematica. My directory structure is simple.

Import["G:\\Research\\Acc and Vel Runs\\5-24\\Mathematica\\Data\\250 \
Acc.xls"][[1]] // TableForm  

That demonstrates the absolute path by using the insert path from the menus. I want this notebook to be portable. I want to give someone the "Mathematica" directory and I want them to be able to run the code. I don't want the paths to break because It will be run on a different machine. Basically I just want to use a relative path starting at the Mathematica level shown above.

share|improve this question
add comment

2 Answers

up vote 17 down vote accepted

If your notebook is in the top directory, you can use

Import[FileNameJoin[{NotebookDirectory[], "path", "to", "your", "file.xls"}]

where the string is the relative path from that directory.

If your notebook is elsewhere in the directory tree and you want to set paths relative to a different directory, then you could define a global $ParentDirectory and then use all paths relative to that by joining strings as in the above example. Then all that the other person needs to do is to set this global value once and they're good. For example:

$ParentDirectory = FileNameJoin[{"absolute", "path", "to", "mathematica"}];
    Import[FileNameJoin[{$ParentDirectory, "path", "to", "your", "file.xls"}]

As Albert Retey points out, you can also use Directory[NotebookDirectory[]] to give you the parent directory of the notebook's directory. In other words, it is an equivalent of cd .. from that directory and can be nested as many times as required.

share|improve this answer
This is exactly what I wanted. But your second suggestion with the "ParentDirectory" is interesting but I don't quite understand it. Could you possibly provide a little more explanation? Isn't the "ParentDirectory" an absolute path? PS I will accept your answer as soon as the timer allows –  Matthew Kemnetz May 24 '12 at 23:04
It is an absolute path. The way you use it is you set $ParentDirectory once and use relative paths from there onwards. When you give it to someone, they set that variable once at the beginning of the notebook/session (or in an init.m file) to what they want it to be and it all works fine. The usage is similar to how Mathematica uses $UserBaseDirectory, $InstallationDirectory, etc. for its stuff. It's hardcoded somewhere, but you can easily change it. This is only in the case where you want to use a different directory than NotebookDirectory[]. If that works, then it's the simplest. –  rm -rf May 24 '12 at 23:12
@MatthewKemnetz This works? I guess I'm a bit surprised, since (judging from the path separators) your operating system appears to be Windows, while R.M.'s appears to be Unix or Mac. The system independent way to do this is with FileNameJoin, as in FileNameJoin[{NotebookDirectory[],"file"}] –  Mark McClure May 25 '12 at 0:38
@MarkMcClure I just presented this as an example, and I guess the OP changed it according to their OS. Your point is valid though, as others might not be so careful. I'll add the OS independent way to my answer... –  rm -rf May 25 '12 at 0:42
@R.M.: you might want to add DirectoryName[NotebookDirectory[]] as a possibility to construct something relative to the current notebook but not necessarily in the "top" directory. Of course that can be Nested as many times as you need... –  Albert Retey May 26 '12 at 16:56
show 1 more comment

Since FileNameJoin was only introduced in Mathematica v7, I thought I'd add a solution using ToFileName for people still running earlier versions (like me):

  NotebookDirectory[], "path", "to", "directory"},

So it works in a similar way, but notice that while the path to the file is enclosed in the {...}, the actual file goes separately.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.