# Why there are line breaks when PutAppend a string to file?

When I tried to putappend a string to a file, I found that in the text there were strange line breaks like:

"\:5927\:79e6\:94c1\:8def,6.190,6.230,6.180,6.220,6.170,6.180,6.190,20683205,\
127957575.000,571122,6.180,1405356,6.170,399300,6.160,686900,6.150,151300,6.1\
40,403200,6.190,337000,6.200,790543,6.210,514700,6.220,523200,6.230,2016-09-1\
4,15:00:00"


It was really disturbing. Can anyone give a solution?

• It appears to be adding line breaks for a long string. If you copy and paste the string into the front end you'll notice that the line breaks disappear. – Edmund Sep 18 '16 at 16:42

The backslash at the end of a line indicates that expression is continued at the next line (and there is no actual linebreak). They are added due to the default option PageWidth -> 78 of OpenAppend:

Options[OpenAppend, PageWidth]

{PageWidth -> 78}


In Mathematica version <10 and starting from version 11.3 setting this option to Infinity solves the problem:

SetOptions[OpenAppend, PageWidth -> Infinity]


In versions 10.0 - 11.3 this method doesn't work due to a bug (fixed in 11.3). So you should write your own PutAppend, for example:

Clear[myPutAppend]
myPutAppend[expr_, pathtofile_String] :=
(Write[#, expr]; Close[#];) &[OpenAppend[pathtofile, PageWidth -> Infinity]]


Put and PutAppend are for exporting Mathematica expressions, where line breaks with continuations are inconsequential. Your needs are likely better served by other tools such as WriteString. No line breaks appear in the output file created here:

x = Range @ 100;
y = Alphabet[];
WriteString["linebreaktest.txt", x, y]


The file contents, abridged:

{1, 2, 3, 4, 5, 6, 7... 96, 97, 98, 99, 100}{a, b, c, d, e, f... v, w, x, y, z}

Don't forget to Close the file after you are done:

Close["linebreaktest.txt"]

• +1 for explaining why Put is not the right choice. I don't think Close is necessary when using a file name (and not a stream) in WriteString. But to append to an existing file, we must first explicitly open it using str = OpenAppend[...], write to str, then finally Close[str]. Just using a file name in WriteString won't append. – Szabolcs Sep 19 '16 at 7:34
• @Szabolcs Thanks for the vote. It seems we have uncovered a version/platform dependence that we should map. In Mathematica 10.1.0 under Windows 7 x64 WriteString["linebreaktest.txt", x]; WriteString["linebreaktest.txt", y] does append, and Close is needed to release the file. Please, which version and platform are you using? – Mr.Wizard Sep 19 '16 at 9:43
• It's not a version difference, it's a misunderstanding on my part. I think the documentation is misleading. I tried it and here's how it works: WriteString["foo.txt", ...] will overwrite foo.txt, not append to it. But as you say, it doesn't close the file. Streams[] reveals an open stream. Any additional writes to this file will therefore keep appending to it, until Close["foo.txt"] is called. – Szabolcs Sep 19 '16 at 9:52
• I was looking at the WriteString examples the other day. Check the first example. It neglects to close the file. I'm going to submit a suggestion to change this because I find it misleading. The other examples are fine. You are right that Close is important. – Szabolcs Sep 19 '16 at 9:53