cleanup writing to file

Replace print >> with open as a context manager.