4

I am currently using the python io library to write in to an external file. Below is a sample piece of code which i am trying to execute:

import io
output=io.StringIO
output.write('\n Hello world ')
output.close()
print output.getvalue() 

when I run this piece of code i get an error. Can any one tell me where I am going wrong and whats the reason for the error.

3
  • How about you tell us what the error is. Commented Feb 4, 2011 at 16:42
  • Also, the StringIO class doesn't write anything to files. It's only an in-memory representation. Commented Feb 4, 2011 at 16:44
  • You're getting the error because, as it says in StringIO's getvalue() documentation, you need to call it before calling the corresponding StringIO object’s close() method. The reason being that the internal string buffer that holds the data written to StringIO object is freed when you call close(). Note also that using StringIO is not the same as writing data to an external file -- everything kept in a memory buffer instead. Commented Feb 4, 2011 at 23:11

4 Answers 4

6

StringIO is for writing to strings, treating them as in-memory streams.

If you want to write to file, do this:

f = open('yourfile', 'w')
f.write('Hello, world.')
f.close()

No need to use StringIO for this.

You did not even get an instance of the class, because there aren't parentheses () after StringIO, so your variable pointed to the StringIO class, and I'm quite sure that is not what you wanted to do.

Sign up to request clarification or add additional context in comments.

Comments

2

I agree with @Andrea. However, if you need to do it your way:

import cStringIO
output=cStringIO.StringIO()

output.write('\n Hello world ')

print output.getvalue()
output.close()

Comments

1

There are plenty of uses for StringIO, for instance it can be used in place of a mutable string. However since your goal is to write to file, you should skip it and just go straight to file:

with open('file/path', 'w') as fh:
    fh.write('Hello World!')

print open('file/path').read() # if you need to actually print it out.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.