Capturing output of os.system to a string

G

gerry.butler

How do I capture output to a string? For example, the output of
os.system('whoami').

I guess I need to redirect stdout, but I'm a total beginner, and I
haven't been able to find out from the tutorials how to do this.
 
L

Lie Ryan

gerry.butler said:
How do I capture output to a string? For example, the output of
os.system('whoami').

I guess I need to redirect stdout, but I'm a total beginner, and I
haven't been able to find out from the tutorials how to do this.

You can't with os.system; use subprocess module instead.
 
G

gerry.butler

Thank you. I'll look at subprocess.

I have since found that commands will do it too, eg,

(status, txt) = commands.getstatusoutput('whoami')

or txt = commands.getoutput('whoami')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top