That bit of code has various problems.
First, you have assigned a class to out instead of an instance. Try out = io.StringIO() instead.
Second, unless you're running Python v3 the write method will complain your are using acsii instead of unicode.
Thirdly, you attempt to read from out after you have closed it. As martineau said, StringIO does not allow to retrieve output after close has been called. Slightly confusing if you're coming from a Java background where close in the corresponding class (StringWriter) has no effect.
Finally, StringIO writes to memory and not to file. Use out = open(filename,'w') if you want to write to file.
So without knowing the exact error your received, that's all I got. Posting the error you receive is normally pretty useful.
StringIO'sgetvalue()documentation, you need to call it before calling the correspondingStringIOobject’sclose()method. The reason being that the internal string buffer that holds the data written toStringIOobject is freed when you callclose(). Note also that usingStringIOis not the same as writing data to an external file -- everything kept in a memory buffer instead.