Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove bytes from StringIO, BytesIO, etc

Tags:

python

I'd like to use a BytesIO object as a continuous buffer (a common use-case). However, is it possible to remove bytes off the head that are no longer needed?

It doesn't seem like it, as there is only a truncate() method.

['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__', '__getstate__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable', 'close', 'closed', 'detach', 'fileno', 'flush', 'getvalue', 'isatty', 'next', 'read', 'read1', 'readable', 'readinto', 'readline', 'readlines', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'writelines']
like image 850
Dustin Oprea Avatar asked Sep 02 '25 03:09

Dustin Oprea


1 Answers

No, you cannot, as BytesIO is an in-memory version of a common file object.

As such it is treated as a sequence of bytes that can be overwritten or appended to, and just like a file removing elements from the front is not efficient as it requires a complete rewrite of all data following.

You probably want to look into the collections.deque() type instead.

like image 191
Martijn Pieters Avatar answered Sep 04 '25 16:09

Martijn Pieters