Fix writing history with output to a file in Python 2

Closes gh-2412