'Confusing error message when passing bytes to print with file pointing to a binary file

The documentation for the print function specifies that the input argument is coerced to string. However, if bytes are passed and the file= parameter points to a file open for binary write, the error message produced is:

>>> out = open('temp', 'wb')
>>> print(b'abc', file=out)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: write() argument 1 must be bytes or buffer, not str

which is, to say the least, confusing, since I clearly passed print a bytes object as argument 1.



Solution 1:[1]

As you said, print coerces its arguments to strings, meaning it is no longer bytes when the write is attempted, but the string "b'abc'". If you're planning to write bytes, out.write(b'abc') is one easy way to do that.

Solution 2:[2]

Use .write() instead of print(). Don't forget to close file after writing.

out = open('temp', 'wb')
out.write(b'abc')
out.close()

Solution 3:[3]

I think you want to write in a file. This is my example code, try this out.

with open('temp','wb') as out:
    out.write(b'abc')

It automatically closes the file, no need to close the file again.

Solution 4:[4]

If you check the documentation in detail here it specifies, that:

Since printed arguments are converted to text strings, print() cannot be used with binary mode file objects. For these, use file.write(...) instead.

From here on out, I'm guessing, what your code is doing is:

  1. Receives a byte string in the print statement
  2. Coerce the byte string into string
  3. Tries to write the string to the file
  4. The file.write() method raises an error, because you are trying to write a string into a file, which is opened in binary write

This can be seen in the error message. Note the err is not coming from the print() method it is raised by the write() method

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jotha
Solution 2 Patrik Sehnoutek
Solution 3
Solution 4