# Writing/exporting files outside the Raspberry Pi home directory

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?

• 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? – DumpsterDoofus Feb 15 '15 at 2:44
• 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. – Mark Adler Feb 15 '15 at 4:15
• @MarkAdler So what permissions level is required for Mathematica to write to it? chmod ---? – iwantmyphd Feb 15 '15 at 4:17
• 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. – Mark Adler Feb 15 '15 at 4:53
• 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. – Mark Adler Feb 15 '15 at 4:55

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.