Writing byte arrays in Jython
Aug 29th, 2005No Comments
I have some byte arrays given to me from a Java API and I want to write it to a file via Jython. I thought it would as easy as:
jpg = open("pic.jpg","wb")
for b in bytes:
jpg.write(b)
jpg.close()
But, of course, it’s not that easy:
Traceback (innermost last): File "getbinarydata.py", line 28, in ? TypeError: write(): 1st arg can't be coerced to String
I ended up having to reach into the Java API thusly:
out = FileOutputStream(fname)
out.write(bytes)
out.close()
Which works, but I think I should be able to do this with “native” Jython calls. But Google shows me no love.
