I'm having trouble with SetDirectory. Practically, I need to get the following code working:

    SetDirectory[StringJoin[NotebookDirectory[], "..\\", "txt_Files\\"]];
<< "2_Vectors_Linear_Combinations.txt";

where the notebook that is running is in a directory, /home/name/Mathematica/project, whose parent directory contains the map txt_Files which contains 2_Vectors_Linear_Combinations.txt. It seems to me that the code is perfectly suited for exactly this situation (I'm trying to run code I did not write, but the map/file structure should be the same and I'm pretty sure the original can't have been this bugged.). Yet the output is:

"Cannot set current directory to \
"/home/name/Mathematica/project/..\\txt_Files \\ "

There was a mistake in my question. However, it speaks to the intelligence of the answerers that the question still got interpreted correctly and answered correctly. I corrected the question so that it really matches the anwsers.

  • $\begingroup$ looks like you are not on Windows -- your code works fine on a Windows machine. For other systems (and it would also work on Windows), can you try to replace the "\\" with "/"? $\endgroup$
    – Stitch
    Feb 28, 2017 at 18:27
  • $\begingroup$ This also works on Linux, so I assume it works on Mac. $\endgroup$
    – Stitch
    Feb 28, 2017 at 18:48

2 Answers 2


The reason for the error is the fact that only Windows uses backslash "\" as a separator in paths. All other systems use forward slash "/". From the error you get it is clear that you are using either Linux or Mac, so you should stick to the forward slash notation. Normally, Windows software allows forward slash usage too (MMA included), so the following solution works universally:

SetDirectory[StringJoin[NotebookDirectory[], "../", "txt_Files/"]];
  • $\begingroup$ @Kuba What I meant under "sometimes" is "some programs". Windows architecture supports both characters, but some software might not (requiring backslash). Since MMA accepts both (i.e. works correctly), we can assume this to be a universal solution. $\endgroup$
    – Stitch
    Feb 28, 2017 at 19:03
  • $\begingroup$ @Kuba Good point! $\endgroup$
    – Stitch
    Feb 28, 2017 at 19:08
  • $\begingroup$ Thanks. Indeed this works (on Linux). Good to know this difference between Windows and Linux. $\endgroup$
    – Kvothe
    Mar 1, 2017 at 9:07

Use FileNameJoin. Either:

SetDirectory @ FileNameJoin[{ParentDirectory @ NotebookDirectory[], "txt_Files"}]


SetDirectory @ FileNameJoin[{NotebookDirectory[], "..", "txt_Files"}]

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.