[issue32520] error writing to file in binary mode - python 3.6.3

Serhiy Storchaka report at bugs.python.org
Mon Jan 8 11:07:38 EST 2018


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

print() converts all its arguments to string by calling str(). The resulting strings are written to the output file.

print(bin_buff, file=fp) is equivalent to

    fp.write(str(bin_buff))
    fp.write('\n')

If you will run Python with options -b or -bb you will get a warning or an error from str(bin_buff) because this operation can be ambiguous. It is better to use an explicit repr() for converting bytes to str if you want to get the representation of the bytes object as a Python literal, or convert it to a string with specifying explicit encoding: str(bin_buff, 'utf-8') or bin_buf.decode('utf-8').

----------
nosy: +serhiy.storchaka
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32520>
_______________________________________


More information about the Python-bugs-list mailing list