Changes three writes into a function, saving code lines.
Changed print >> outfile to outfile.writelines()

Not sure if it matters, but since the print >> has been dropped
in Python 3, I refactored it to suit.