The order of escaping was wrong

I fixed it
Adding to the file, instead of overwriting

This allows me to write the result of processing multiple
files to the same output file.
Added documentation string