# Writing to file as it goes [duplicate]

I was wondering if there was a way to write data to a file, during a Do loop, without having to wait till the end.

I have written a code which scans over many parameters which get plugged into many formulae. I then have a condition on a particular output value which decides if I append the data point to my list.

I run a 'Do' loop for a certain number of iterations, and each time the condition is met the values of that point gets appended to my list.
At the end of the Do loop, i export the list to a .txt file.

Is there a way to get Mathematica to write the data point values to a .txt file as it runs the Do loop... so that it is constantly updating the list?

The reason I was hoping to so this is that if the computer crashes (say after its been running for a whole day!) then I don't lose all the points as they remain in the txt file.

I tried to put the export command in the Do loop but mathemtica didn't seem to like that.

I'm new to mathematica, I've only been using it for the last two weeks... i've tried googling and searching posts on this site for some information but can't find anything... maybe that's because this feature doesn't exist?... if someone could let me know either way that would be great cheers!

-

## marked as duplicate by Mr.Wizard♦Apr 22 at 17:19

Hi there. This question looks pretty close to what you want. –  cormullion Aug 24 '13 at 19:24
as i read it he wants to append to the same file each time (?). For that see OpenWrite[], WriteString[], and related functions. –  george2079 Aug 24 '13 at 19:53
Related: (13306), (27189). See also: (4683), (10456), (30489) –  Mr.Wizard Apr 22 at 17:29

You mean something like this? The write should be flushed to disk after each Write, hence I do not think you need to close and reopen the file inside the loop using Append. But it is easy to make it do that if needed.

Also, if you are on Linux/Unix system, or windows with cygwin and such, you can type the command

tail -f log.txt


in a separate window and view the file being updated as Mathematica is running inside the loop.

SetDirectory[NotebookDirectory[]];
lst = Table[i, {i, 100}];
o = OpenWrite["log.txt"];

If[o === $Failed, Print["failed to open file"] , Do[ If[PrimeQ[lst[[n]]], Write[o, lst[[n]]]] , {n, Length[lst]} ]; Close[o] ]  Here is a version that closes the file after each write, and open it as append. SetDirectory[NotebookDirectory[]]; lst = Table[i, {i, 100}]; Do[ If[PrimeQ[lst[[n]]], o = OpenAppend["log2.txt"]; If[o ===$Failed, Throw[o]];

Write[o, lst[[n]]];
Close[o]
]

, {n, Length[lst]}
];


check

>cat log.txt
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
>

-
thanks for the help Guys! It seems all I actually needed was 'PutAppend' instead of 'AppendTo' and 'Export' - sorry i'm new to this so I don't know some of the obvious stuff. But going through your example up there taught me quite a few useful things I didn't know so its all good :) Thanks again –  kingmonkeywoman Aug 25 '13 at 16:22
@kingmonkeywoman if you have a good/better answer you should post it as an answer. –  george2079 Aug 25 '13 at 22:27