Capturing output of os.system to a string

Discussion in 'Python' started by gerry.butler, Nov 24, 2009.

  1. gerry.butler

    gerry.butler Guest

    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.
     
    gerry.butler, Nov 24, 2009
    #1
    1. Advertising

  2. gerry.butler

    Lie Ryan Guest

    gerry.butler wrote:
    > 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.
     
    Lie Ryan, Nov 24, 2009
    #2
    1. Advertising

  3. gerry.butler

    gerry.butler Guest

    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')
     
    gerry.butler, Nov 24, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Strahl [MVP]

    Capturing ASPX output from another page

    Rick Strahl [MVP], Jul 9, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    484
    Steve C. Orr, MCSD
    Jul 9, 2003
  2. Andy Fish

    capturing the output of a JSP

    Andy Fish, Feb 11, 2004, in forum: Java
    Replies:
    4
    Views:
    2,008
    Chris Smith
    Feb 11, 2004
  3. Replies:
    0
    Views:
    394
  4. Replies:
    2
    Views:
    527
  5. Hoegje
    Replies:
    2
    Views:
    22,554
    Gianni Mariani
    Dec 5, 2003
Loading...

Share This Page