It is already in memory. Loading a pickle returns a python structure.
Moreover, a StringIO
object is a in-memory file-like object, not a Python object structure. You cannot take the in-memory representation of a python structure and 'write' it into memory, you instruct the Python interpreter to construct those objects for you (which is what the pickle
module does for you).
Last but not least, you really should avoid using atexit
to close files. File objects that are still open when Python exits are automatically closed. Even if Python doesn't this for some reason, the OS would do it anyway.