Use file.append to write
Merge pull request #18774 from rallytime/cp-18150

Backport #18150 to 2014.1