+ write to the output file when provided

When the second argument (output file) is provided, write into that file. I added this since writing into a text file in Python is more robust with unicode characters.