I wanna be able to start my session by loading a bunch of data and then finish by saving everything.

Here's an example of what I've been doing: I have the function g[x_] := g[x] = x^2, I calculate g[1] and g[2], and I run Save["data.m", {g}]. Next session I load with Get["data.m"], and then I calculate g[3] and g[4] and save, intending to get a file with g[x_] as well as g[1] through g[4].

But as it turns out, Save appends rather than overwrites, and it doesn't just append the new stuff. So it'll contain g[1], g[2], g[x_], g[1], g[2], g[3], g[4], g[x_].

What's the best approach here? Should I just overwrite the file when I save, and if so, is there a better way than DeleteFile["data.m"]? Is there a way to append but only the new stuff?

  • "Is there a way to append but only the new stuff?"

No, not that I know of. Of course you could save to a new file. The new file should contain both the new and old definitions, without replication.

  • "Should I just overwrite the file when I save?"

You can, since the contents of the file have been loaded in memory and will be saved out again. But it may be safer to Save to a new file in case something goes wrong, particularly if calculation of each value is time-consuming. Devise a tagging method (e.g. programmatically use the current date and time in the file name, see e.g. CurrentDate)

  • "Is there a better way than DeleteFile["data.m"]?"

You could call an OS specific command, but DeleteFile is pretty convenient...

See also:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.