[Tutor] extracting a cPickle/pickle file from inside a zip file

Danny Yoo dyoo at hashcollision.org
Fri Aug 29 21:32:47 CEST 2014


> now if i do this
>
> outfile = cStringIO.StringIO()
> outfile.write(pyfromzip)
> z=cPickle.load(outfile)


Do you have to rewind the outfile so that the read is properly
positioned?  The following interaction:

#############################
>>> import StringIO
>>> out = StringIO.StringIO()
>>> out.write("hello world")
>>> out.read()
''
#############################

shows that if we immediately read what you write, we get nothing: the
file position is not at the beginning.  But as soon as we do a seek():


##############################
>>> out.seek(0)
>>> out.read()
'hello world'
##############################

then we're good.



Alternatively, can you do this instead?

#########################################
>>> out = StringIO.StringIO("hello, this is a test")
>>> out.read()
'hello, this is a test'
#########################################

In your case, construct the StringIO object with the content of
pyfromzip, up front, rather than with a separate call to write().


More information about the Tutor mailing list