Skip to main content

files and directories - Change Temporary Directory


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 !



Answer



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:



Close[s]


(* "/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".


Comments