PutAppend with PageWidth -> Infinity
In Mathematica using of the PutAppend
command is the most straightforward way to maintain a running log file with results of intermediate computations. But it uses by default PageWith->78
setting when exporting expressions to a file and so there is no guarantee that every intermediate output will take only one line in the log.
PutAppend
does not have any options itself but tracing its evaluations reveals that it is based on the OpenAppend
function which has the PageWith
option and allows changing its default value by the SetOptions
command:
In[2]:= Trace[x>>>"log.txt",TraceInternal->True]
Out[2]= {x>>>log.txt,{OpenAppend[log.txt,CharacterEncoding->PrintableASCII],OutputStream[log.txt,15]},Null}
So we can get PutAppend
to append only one line at a time by setting:
SetOptions[OpenAppend, PageWidth -> Infinity]
UPDATE
There is a bug introduced in version 10 (fixed in version 11.3): SetOptions
no longer affects the behavior of OpenWrite
and OpenAppend
.
A workaround is to implement your own version of PutAppend
with explicit PageWidth -> Infinity
option:
Clear[myPutAppend]
myPutAppend[expr_, pathtofile_String] :=
(Write[#, expr]; Close[#];) &[OpenAppend[pathtofile, PageWidth -> Infinity]]
Note that we also may implement it via WriteString
as shown in this answer, but in this case it will be necessary to preliminarily convert the expression into the corresponding InputForm
via ToString[expr, InputForm]
.