The following was the code:


The output was



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 ?

  • 2
    $\begingroup$ 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. $\endgroup$
    – Bernd
    Commented Jun 22, 2013 at 14:09
  • $\begingroup$ 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. $\endgroup$
    – Kuba
    Commented Jun 22, 2013 at 14:13
  • 4
    $\begingroup$ 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" $\endgroup$ Commented Jun 22, 2013 at 15:00
  • 2
    $\begingroup$ Use FileNameJoin[{Directory[], "lala", "hey", "folder", "file.gif"}] and forget about it $\endgroup$
    – Rojo
    Commented Jun 22, 2013 at 16:47
  • $\begingroup$ Thanks @Rojo for the FileNameJoin. That solves it 'elegantly' $\endgroup$ Commented Jun 28, 2013 at 9:00

1 Answer 1


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[]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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