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

wallenpb at gmail.com wallenpb at gmail.com
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=open(filename,"wb")
    self.out.write("BM")          # magic number



This the is the error output from Python:
------------------------------------
Traceback (most recent call last):
  File "py_mandel.py", line 19, in <module>
    my_bmp=kohn_bmp("out.bmp",image_width,image_height,3)
  File "C:\Python30\py_kohn_bmp.py", line 47, in __init__
    self.out.write("BM")          # magic number
  File "C:\Python30\lib\io.py", 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