In Java, I can read the stdout as a string using
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
System.setOut(new PrintStream(stdout));
String toUse = stdout.toString();
/**
* do all my fancy stuff with string `toUse` here
*/
//Now that I am done, set it back to the console
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
Can someone please show me an equivalent way of doing this in python? I know different flavors of this question has been asked a number of times, such as Python: Closing a for loop by reading stdout and How to get stdout into a string (Python). But I have a feeling that I don't need to import subprocess to get what I need, as what I need is simpler than that. I am using pydev on eclipse, and I program is very simple.
I already tried
from sys import stdout
def findHello():
print "hello world"
myString = stdout
y = 9 if "ell" in myString else 13
But that does not seem to be working. I get some compaints about opening file.