[utils] Fix encoding/decoding handling when writing to binary/text files

4 jobs for py2_py3_fixes in 19 minutes and 19 seconds (queued for 10 minutes and 17 seconds)