How to change the value of $TemporaryDirectory ? I need to change it because there is not enough space on the C drive and the calculation seems to stop after a while !

  • $\begingroup$ TempDir is the default directory for windows and not only for mathematica. It seems mathematica doesn't create anything automatically in this directory except using createdirectory and so on without specific path. Are you sure that mathematica writes right in this directory? The simplest thing you can try to redirect input is to use setdirectory to the folder that you want. $\endgroup$
    – funnyp0ny
    Oct 20, 2014 at 9:23
  • $\begingroup$ What kind of operation do you have problems with? $\endgroup$
    – Yves Klett
    Oct 20, 2014 at 9:23
  • $\begingroup$ Hi, welcome to Mathematica.SE, please consider taking the tour so you learn the basics of the site. Once you gain enough reputation by making good questions you will be able to vote up and down both questions and answers. When you see good ones, please vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. As you receive help, try to give it too, by answering questions in your area of expertise. $\endgroup$
    – rhermans
    Oct 20, 2014 at 9:33
  • $\begingroup$ If you are in Windows, on System Properties, Environment Variables, you could change the TMP and TEMP variables to point to other folders. But do this carefully and only after understanding what you are doing. Anyhow, this seems to be off-topic and its likely to be closed. $\endgroup$
    – rhermans
    Oct 20, 2014 at 9:36
  • 1
    $\begingroup$ I do not really get the downvote. The question does raise an interesting point (even if most users never come across the issue). $\endgroup$
    – Yves Klett
    Oct 20, 2014 at 14:47

2 Answers 2


Just set $TemporaryDirectory to a desired directory. You can use system commands such as FileNameJoin or FileNameSetter to create a directory name appropriate to your system. I have a unix-based Mac, so I ran an example using

$TemporaryDirectory = "/tmp/math"

Here is the example from the docs for OpenWrite, executed under this new setting.

Create and open a new stream for writing:

fname = FileNameJoin[{$TemporaryDirectory, "testfile"}];

s = OpenWrite[fname]

(* OutputStream["/tmp/math/testfile", 145] *)

Write an expression to the stream:

Write[s, Expand[(x + y)^10]]

Close the stream:


(* "/tmp/math/testfile" *)

And so on.

Caveat: On my system, the temporary directory used for

s = OpenWrite[]

(from the docs for $TemporaryDirectory) is not $TemporaryDirectory, but some file in a random-looking sub-sub-directory: "/var/folders/9d/68khy4s15sjf9qfpnhqz9tnc0000gn/T/m000003840331".

  • $\begingroup$ What version of Mma are you running? According to the docs, prior to 7.0 the value of $TemporaryPrefix was used for OpenWrite[] and CreateDirectory[]. $\endgroup$
    – dionys
    Oct 21, 2014 at 8:46
  • $\begingroup$ @dionys V10.0.1, Mac OSX 10.9.5. Good call - it is in fact $TemporaryPrefix that's being used. Am I the only one? $\endgroup$
    – Michael E2
    Oct 21, 2014 at 11:32
  • $\begingroup$ It is the same on my system, V10.0.1, Windows x64 .. defaults determined by $TemporaryPrefix (despite what's stated in the docs). $\endgroup$
    – dionys
    Oct 21, 2014 at 12:15

I got the answer from Mathematica Forum on Wolfram website.

"If you wish to change the value of $TemporaryDirectory for a particular Mathematica session then you can simply execute

$TemporaryDirectory="path to a new directory" 

since $TemporaryDirectory is not Protected"

Thanks to David Reiss

  • 3
    $\begingroup$ Um, that's what I said... $\endgroup$
    – Michael E2
    Oct 21, 2014 at 3:41
  • 2
    $\begingroup$ You should consider accepting @MichaelE2´s answer - he proposed the same solution 12hrs earlier... $\endgroup$
    – Yves Klett
    Oct 21, 2014 at 4:54
  • $\begingroup$ Thanks a lot Michael. Yes, you're right. Sorry, I'm new here. I didn't know that there is "accept answer" button. $\endgroup$ Oct 22, 2014 at 4:20
  • $\begingroup$ I don't have enough reputation to Vote up your answer :( $\endgroup$ Oct 22, 2014 at 4:22

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.