# Printing data into a file inside the loop one by one [duplicate]

Suppose I have a simple loop

dat=Table["i",{i,1,10}];


Now once the run is finished, I can store the data using

Export["dat.txt",dat]


It works perfectly. However suppose I have a bigger loop with complicated evaluations which takes long time to complete. And I need to use the output upto i=5 before the run is complete. What I mean is that data should be stored in "dat.txt", when i=1 is complete. Then in the next line the file includes the output for i=2. This helps me to use the output file even if some problem related to computer hang problem appears and the code finishes prematurely.

Can someone enlighten me regarding the method I should use for that?

• does not work for multiple columns, also the output format given is inside inverted comma, I can't use those further. Feb 10 at 15:29

In the Table, wrap your output in e.g. PutAppend so each time an output is generated it gets appended to a file. I recommend that you label the results with the value of the iterator as well.

Here is a minimal example, where I use Pause to simulate a long computation, and Print just to provide visual feedback to you of the progress of the computation (neither would be needed in your code):

Table[
PutAppend[
Print[i]; Pause[10]; {i, "results from iteration " <> ToString[i]},
"runningOutput.txt"
],
{i, 0, 5}
]


If you open the file in an editor such as Notepad++, you will be notified of the changes to the file and see the updates in real time.

Note that it would be up to you (or to extra code that you can write) to clear the file before each run, if you want to.