pythoniobytesio

Getting number of bytes from BytesIO


I want to get the exact number of bytes from a BytesIO object, the same bytes that read() returns.

The closest way to do it I found is:

readable = io.BytesIO(b'\xff\xd8')
print(sys.getsizeof(readable))

However, instead of 2 got 48 as a result.


Solution

  • io.BytesIO is a stream. Consume it to get the content's size:

    >>> sys.getsizeof(io.BytesIO(b'\xff\xd8').getvalue())
    35
    

    and use len() instead of sys.getsizeof if you want to get its length:

    >>> len(io.BytesIO(b'\xff\xd8').getvalue())
    2
    

    or read a specific number of bytes manually, so you'll know the size beforehand:

    >>> io.BytesIO(b'\xff\xd8').read(2)
    b'\xff\xd8'
    >>> sys.getsizeof(io.BytesIO(b'\xff\xd8').read(2))
    35
    >>> len(io.BytesIO(b'\xff\xd8').read(2))
    2
    

    Since this is a stream, size can't be known before consuming the stream.

    The reason you don't get 2 with your code is because you're asking the size of the BytesIO instance instead of the length of the stream's value:

    >>> readable = io.BytesIO(b'\xff\xd8')
    >>> readable
    <_io.BytesIO object at 0x0000024A8CFC3BD0>
    >>> sys.getsizeof(readable)
    115
    >>> len(readable.getvalue())
    2