error writing str to binary stream - fails in Python 3.0.1, works in 2.x

wallenpb at wallenpb at
Mon Mar 16 16:54:04 CET 2009

I am working with a bit of code that works ok in Python 2.x (tested
fine in 2.5.4 and 2.6.1) but fails in 3.0.1.
The code opens a file for binary output to witht the objective to
produce a .bmp graphics file.  The code below illustrates the first of
several like errors when a str object is attempted to be written to
the binary file.  From what I have seen, this was a fairly common
technique prior to 3.0.1 being released so I am assuming the type
checking is tighter with the new version.  What is the proper way of
doing this now, or the work around?  Any help appreciated.  -- Bill

the code:
    self.out.write("BM")          # magic number

This the is the error output from Python:
Traceback (most recent call last):
  File "", line 19, in <module>
  File "C:\Python30\", line 47, in __init__
    self.out.write("BM")          # magic number
  File "C:\Python30\lib\", line 1038, in write
    raise TypeError("can't write str to binary stream")
TypeError: can't write str to binary stream

More information about the Python-list mailing list