1

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)
sdikby
  • 1,383
  • 14
  • 30

1 Answers1

3

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
zipa
  • 27,316
  • 6
  • 40
  • 58
  • i think _.tell()_ would give me the number of elements in temp. I would have the size of temp in byte, like when you are using _os.path.getsize(file)_ – sdikby Apr 21 '17 at 09:34
  • I would accept your answer as it answer my question, but with seek() would it rewind temp from the beginning, as i am concatenating XML files and i would write the my temp file to disk only if it have a certain size. I fear that with seek, it will reset my temp each time and i will never get to pass the if statement. – sdikby Apr 21 '17 at 09:50
  • It depends how you construct your loop – zipa Apr 21 '17 at 09:52