# Difference between Directory[] and NotebookDirectory[] .. a trailing /?

The following was the code:

Directory[]
NotebookDirectory[]


The output was

"/media/Dataholder/final"

"/media/Dataholder/final/"


In the second case, there is a trailing "/" (I am on Mathematica 8 on Ubuntu)

Just curious, why is there a difference ? Is is specific to my machine ?

I found this when this was breaking the code, wherein I wanted to change the working directory to another one relative to the current directory, i.e. using

simFldr = Directory[] <> "anotherSubDirectory"


Though this sounds trivial, the patchwork needed to get to that directory will be non trivial since I have a code which must work both on Windows and Unix.

Any smart solution to set the directory to a subdirectory ?

• Maybe this is of help? Relative paths for portable notebooks in Mathematica - i.e. regarding the directory to work on both Windows and Linus, not as a reply to the difference between the two commands. Jun 22 '13 at 14:09
• Bernd is right, ToFileName does not care about \$PathnameSeparator. Moreover, notice that Directory[] is not the same as NotebookDirectory[] unless You SetDirectory@NotebookDirectory[], well, at least it is so on Win.
– Kuba
Jun 22 '13 at 14:13
• I suppose you can always use FileNameJoin. I usually stick with StringJoin though as it is so much easier to type. Anyway, an example: FileNameJoin[{"/Users/", "/Jacob"}]-> "/Users/Jacob" Jun 22 '13 at 15:00
• Use FileNameJoin[{Directory[], "lala", "hey", "folder", "file.gif"}] and forget about it
– Rojo
Jun 22 '13 at 16:47
• Thanks @Rojo for the FileNameJoin. That solves it 'elegantly' Jun 28 '13 at 9:00

You can use FileNameJoin to assemble file names in a robust and platform-independent way. This will solve the difficulties with the trailing /.

You can also use AbsoluteFileName to bring a path to canonical form. This works only with paths that exist.

For example, to compare Directory[] and NotebookDirectory[], you can use

AbsoluteFileName@Directory[] === AbsoluteFileName@NotebookDirectory[]