获取 StringIO 对象的字节大小

get the size in bytes of a StringIO object

是否可以获取 StringIO 对象的字节大小?或者每次我想获取它的大小时我都应该将它写入磁盘?
我搜索了很多但没有成功。 .sizeof() 但我不认为那是我想要的。

我正在遍历文件名列表

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)

你应该寻找到最后并使用tell():

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

示例:

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