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']
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With