RE: Redirecting system output quietly

Discussion in 'Python' started by Eddie Parker, Nov 16, 2004.

  1. Eddie Parker

    Eddie Parker Guest

    Look at the 'popen' modules.

    (I believe their under the os module).

    -e-

    -----Original Message-----
    From: python-list-bounces+eddie=
    [mailto:python-list-bounces+eddie=] On Behalf Of
    Madhusudhanan Chandrasekaran
    Sent: November 15, 2004 5:03 PM
    To:
    Subject: Redirecting system output quietly

    Hi,
    I am a newbie here. I want to know how to redirect os.system("ls -af")
    to a string variable without it barfing the answer of ls -af on the
    screen.

    Thanks in advance,
    Madhusudhanan.C
    --
    http://mail.python.org/mailman/listinfo/python-list

    ---
    Incoming mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.788 / Virus Database: 533 - Release Date: 01/11/2004


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.788 / Virus Database: 533 - Release Date: 01/11/2004
    Eddie Parker, Nov 16, 2004
    #1
    1. Advertising

  2. Eddie Parker

    StvB Guest

    cmdline = 'whatever command you want to run'
    pipe = os.popen(cmdline + " 2>&1")
    for line in pipe.readlines():
    tempList.append(line)
    rc = pipe.close()
    if rc:
    ErrorResult = 1
    else:
    fg.ErrorResult = 0



    "Eddie Parker" <> wrote in message
    news:...
    > Look at the 'popen' modules.
    >
    > (I believe their under the os module).
    >
    > -e-
    >
    > -----Original Message-----
    > From: python-list-bounces+eddie=
    > [mailto:python-list-bounces+eddie=] On Behalf Of
    > Madhusudhanan Chandrasekaran
    > Sent: November 15, 2004 5:03 PM
    > To:
    > Subject: Redirecting system output quietly
    >
    > Hi,
    > I am a newbie here. I want to know how to redirect os.system("ls -af")
    > to a string variable without it barfing the answer of ls -af on the
    > screen.
    >
    > Thanks in advance,
    > Madhusudhanan.C
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    > ---
    > Incoming mail is certified Virus Free.
    > Checked by AVG anti-virus system (http://www.grisoft.com).
    > Version: 6.0.788 / Virus Database: 533 - Release Date: 01/11/2004
    >
    >
    > ---
    > Outgoing mail is certified Virus Free.
    > Checked by AVG anti-virus system (http://www.grisoft.com).
    > Version: 6.0.788 / Virus Database: 533 - Release Date: 01/11/2004
    >
    >
    >
    StvB, Nov 16, 2004
    #2
    1. Advertising

  3. There's an even easier way to do this. You can use the "commands"
    module:

    http://www.python.org/doc/current/lib/module-commands.html

    This is easier and faster than using popen, and will allow you to get
    exit status (os.popen* won't let you)

    Hugo


    > > Hi,
    > > I am a newbie here. I want to know how to redirect os.system("ls -af")
    > > to a string variable without it barfing the answer of ls -af on the
    > > screen.
    > >
    > > Thanks in advance,
    > > Madhusudhanan.C
    Hugo Gonz?lez, Nov 16, 2004
    #3
  4. Eddie Parker

    David Bolen Guest

    (Hugo Gonz?lez) writes:

    > There's an even easier way to do this. You can use the "commands"
    > module:
    >
    > http://www.python.org/doc/current/lib/module-commands.html
    >
    > This is easier and faster than using popen, and will allow you to get
    > exit status (os.popen* won't let you)


    Well, since the commands module is just a simple wrapper around the
    os.popen() call, it's hard to see it giving you anything you can't get
    with the call itself :)

    You can certainly get the exit status from os.popen - it is returned
    as the result of the close() operation. (Under Windows, for the
    higher order popen* calls, you get the exit status from the close() on
    the final of the multiple handles you close, while under Unix I think
    you need to explicitly do a wait() on the returned handle).

    One thing to be aware of with commands (although this shouldn't be a
    problem for the OP in this case), is that it is Unix only, and won't
    work properly under Windows.

    -- David
    David Bolen, Nov 16, 2004
    #4
  5. Eddie Parker

    Donn Cave Guest

    In article <>,
    (Hugo Gonz?lez) wrote:

    > There's an even easier way to do this. You can use the "commands"
    > module:
    >
    > http://www.python.org/doc/current/lib/module-commands.html
    >
    > This is easier and faster than using popen, and will allow you to get
    > exit status (os.popen* won't let you)


    You can look at the implementation, it's a Python source file
    commands.py in the library directory. I mention that because
    you might be interested to see how it uses os.popen, and gets
    the status.

    Donn Cave,
    Donn Cave, Nov 16, 2004
    #5
    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. Soren Mikkelsen
    Replies:
    2
    Views:
    2,446
    Thomas Scheidegger [MVP]
    Jul 8, 2005
  2. Jeffry Johnston
    Replies:
    0
    Views:
    603
    Jeffry Johnston
    Nov 7, 2003
  3. heinz

    Should XML quietly die?

    heinz, Nov 9, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    402
    Mark Rae
    Nov 12, 2006
  4. Peng Yu
    Replies:
    1
    Views:
    332
    Ron Bergin
    Dec 31, 2009
  5. Rodrick Brown
    Replies:
    2
    Views:
    425
    Stefan Behnel
    Apr 17, 2013
Loading...

Share This Page