Fix automatic newline conversion by Python by writing in binary mode.