StringIO replacement that works with bytes instead of strings?

sorin picture sorin · Jun 25, 2011 · Viewed 43k times · Source

Is there any replacement for python StringIO class, one that will work with bytes instead of strings?

It may not be obvious but if you used StringIO for processing binary data you are out of luck with Python 2.7 or newer.

Answer

senderle picture senderle · Jun 25, 2011

Try io.BytesIO.

As others have pointed out, you can indeed use StringIO in 2.7, but BytesIO is a good choice for forward-compatibility.