1
$\begingroup$

Assuming I already have the file created.

I have a programme that,returns an output, in our case number. I want Mathematica to save this number in a file, together with results saved from previous calculations.

After having checked Mathematica commands, I found that I could do something like:

file = OpenWrite["test.dat"];
Write[file, new];
Close[file];

However, this doesn't work, because it overwrites the information that is already in the file. Working with this limitation, I tried a different approach:

results=Import["test.dat", "Table"];
AppendTo[results,newresult];
file = OpenWrite["test.dat"];
Write[file,results];
Close[file];

This new approach still doesn't work since when I initialize the file test.dat with

Export["test.dat", Range[1, 10]]

and then import the list, I do not get the same list, but

{{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}}

I get the same unwanted list, even if I add the "Table" option to Export, or Import...

How do I efficiently save the progress of my programme in the file test.dat?

Any help would be appreciated.

$\endgroup$
8
  • 1
    $\begingroup$ See Put and PutAppend $\endgroup$ Nov 20, 2014 at 12:18
  • $\begingroup$ Related: "The best way to construct a function with memory." $\endgroup$ Nov 20, 2014 at 12:25
  • $\begingroup$ @MariusLadegårdMeyer The PutAppend also seems to overwrite the info, and doesn't solve the import data problem $\endgroup$ Nov 20, 2014 at 12:29
  • 1
    $\begingroup$ @Anoldmaninthesea, try this: (# >>> "test.dat") & /@ {1, 2, 3} followed by ReadList["test.dat"]. For me, this returns {1,2,3}. If you now want to add some new numbers, say (# >>> "test.dat") & /@ {4, 5, 6, 7, 8}, you will find that ReadList["test.dat"] gives you {1,2,3,4,5,6,7,8}. If you only need to save numbers/lists, and not expressions, then this is the simplest way I can think of. $\endgroup$ Nov 20, 2014 at 12:41
  • 1
    $\begingroup$ @Anoldmaninthesea, I can't reproduce that. Did you save it both times with >>>? Try starting from a new file name. $\endgroup$ Nov 20, 2014 at 13:25

2 Answers 2

2
$\begingroup$

For simply saving numbers or lists to file, I personally prefer to use Put/>> and PutAppend/>>> to store data, and ReadList to retrieve it. For instance:

(# >>> "file.dat") & /@ {1,2,3};
ReadList["file.dat"]
(* {1,2,3} *)
(# >>> "file.dat") & /@ {1,2,3,4,5};
ReadList["file.dat"]
(* {1,2,3,1,2,3,4,5} *)

You could obviously also store the lists each time and then Join them if you want:

{1, 2, 3} >>> "file.dat";
{4, 5, 6, 7} >>> "file.dat";
Join @@ ReadList["file.dat"]
(* {1, 2, 3, 4, 5, 6, 7} *)
$\endgroup$
5
$\begingroup$

Open the file with OpenAppend not OpenWrite. Any Writes will then append to the file not overwrite.

$\endgroup$
2
  • $\begingroup$ this is especially useful in combination with ExportString for text like formats where you can append line by line, e.g. CSV. $\endgroup$ Nov 21, 2014 at 8:21
  • $\begingroup$ Many thanks for your answer Ymareth $\endgroup$ Nov 21, 2014 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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