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.
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.
Copyright © 2021 Jogjafile Inc.
io.BytesIOis a stream. Consume it to get the content's size:and use
len()instead ofsys.getsizeofif you want to get its length:or read a specific number of bytes manually, so you'll know the size beforehand:
Since this is a stream, size can't be known before consuming the stream.
The reason you don't get
2with your code is because you're asking the size of theBytesIOinstance instead of the length of the stream's value: