Bytearray disk size

Asked

Viewed 34 times

1

Suppose I convert some image to bytearray.

with open("img.png", "rb") as imageFile:
  f = imageFile.read()
  b = bytearray(f)

print b[0]

How do I know how much disk space this bytearray will take over if I decide to save you.

1 answer

3


You can use the native function len() to calculate the size in bytes of a bytearray, look at you:

with open("img.png", "rb") as imageFile:
    f = imageFile.read()
    b = bytearray(f)

print(len(b))

Exit:

384373

Comparing:

$ ls -al img.png 
-rw-rw-r-- 1 lacobus lacobus 384373 Jul  3 17:24 img.png
  • I thought that len() would return the number of entries.

  • @Exact Matheussaraiva, but if it’s a byte array, the number of elements will be the number of bytes, agree?

  • True, but I thought bytearray in that case it would have more than one byte per position.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.