get the size in bytes of a StringIO object

4.1k Views Asked by At

Is it possible to get the size in bytes of a StringIO object? or should i write it to disk every time i want to get its size?
I have searched a lot but without success. There .sizeof() but i don't think that is what i want.

I am iterating over a list of filenames

temp=StringIO()
for idx,filename in enumerate(filenames):
    temp.write('something')
    if ((temp.__sizeof__()+os.path.getsize(os.path.join(inputf,filenames[idx])))/1048576.0 >= 128.0):
        (do something)
1

There are 1 best solutions below

3
zipa On BEST ANSWER

You should seek to the end and use tell():

temp.write('something')
temp.seek(0, os.SEEK_END)
if ((temp.tell()+...

Example:

from StringIO import StringIO
import os
temp = StringIO()
temp.write("abc€")
temp.seek(0, os.SEEK_END)
print temp.tell() #6