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.

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 ?

share|improve this question
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. –  Bernd 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" –  Jacob Akkerboom 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' –  my account_ram Jun 28 '13 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 /.

share|improve this answer

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.