When running Mathematica (v10) on my Raspberry Pi, I'd like to output an image file to a directory other than /home/pi. I understand that letting Mathematica write files anywhere arbitrarily is a bad idea, but I'd like to be able to send it somewhere other than /home/pi.

Typically, if I want to write/move/copy a file somewhere I need the sudo command, but that does not appear to be an option with Export.

I assume the code should look like the following (to be sent to /dir1/dir2/dir3):

Export["/dir1/dir2/dir3/example.jpg", output, "jpg"]

but this returns an error:

Export::nodir: Directory /home/pi/dir1/dir2/dir3/ does not exist

Export:noopen: Cannot open /dir1/dir2/dir3/example.jpg.

What do I need to do to export my file to an arbitrary directory?

  • $\begingroup$ Assuming all the normal commands work on Pi, you can just do SetDirectory["/dir1/dir2/dir3/"] and then export normally using Export["example.JPG", output]. Does that work for you? $\endgroup$ Feb 15, 2015 at 2:44
  • 1
    $\begingroup$ No, you don't need SetDirectory. If you have permissions to write in the directory, and it exists, then you can write the file using an absolute path as in your example. You should not be "typically" using sudo. Create the directories you want and give them the permissions necessary outside of Mathematica to allow pi to write to them. $\endgroup$
    – Mark Adler
    Feb 15, 2015 at 4:15
  • $\begingroup$ @MarkAdler So what permissions level is required for Mathematica to write to it? chmod ---? $\endgroup$
    – iwantmyphd
    Feb 15, 2015 at 4:17
  • $\begingroup$ It depends on the owner of the directory. If pi is the owner, then chmod u+rwx dir. pi should be the owner for new directories you create for this purpose. If necessary, chown can be used to set the owner. $\endgroup$
    – Mark Adler
    Feb 15, 2015 at 4:53
  • $\begingroup$ If it is an existing directory in the system (which by the way means you probably should not be writing to it), then you would need to make sure its group is one of pi's, and then chmod g+rwx dir. $\endgroup$
    – Mark Adler
    Feb 15, 2015 at 4:55

1 Answer 1


Although this may not be the best answer, it seems to work for me. Since I don't want to change the permissions of the existing directory to allow Mathematica to write to it, but I want to be able to have those files accessible from that directory, I am making a symbolic (soft) link between the directory in /home/pi/ to the target directory by doing the following:

sudo ln -s /home/pi/directory /dir1/dir2/dir3

in order to link the contents of /home/pi/directory with /dir1/dir2/dir3.

I realize that this solution probably belongs in the Unix & Linux SE, but unless someone can come up with a way to do this within Mathematica without messing with permissions and risking security issues, this is what I'll be using.


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.