On Mon, Nov 23, 2009 at 5:27 PM, gerry.butler <span dir="ltr"><<a href="mailto:gabutler@acslink.net.au">gabutler@acslink.net.au</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
How do I capture output to a string? For example, the output of<br>
os.system('whoami').<br>
<br>
I guess I need to redirect stdout, but I'm a total beginner, and I<br>
haven't been able to find out from the tutorials how to do this.<br>
<font color="#888888"><br></font></blockquote><div> </div><div>You don't; os.system is only usable to send commands off that need no input and which you don't care for the output. Check out the subprocess module. </div>
<div><br></div><div>You can do like:</div><div><br></div><div>popen = subprocess.Popen("whoami", stdout=subprocess.PIPE)</div><div><br></div><div>Then "out, err = popen.communicate()"</div><div><br></div>
<div>The 'out' should contain the output from the command.</div></div><div name="mailplane_signature"><br></div><div name="mailplane_signature">--S</div>