running a shell command from a python program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sandman

    #1

    running a shell command from a python program

    Hi,
    I'm a newbie, so please be gentle :-)

    How would I run a shell command in Python?

    Here is what I want to do:
    I want to run a shell command that outputs some stuff, save it into a
    list and do stuff with the contents of that list.

    I started with a BASH script actually, until I realized I really needed
    better data structures :-)

    Is popen the answer? Also, where online would I get access to good
    sample code that I could peruse?

    I'm running 2.2.3 on Linux, and going strictly by online doc so far.

    Thanks!
    S C

  • Thomas Guettler

    #2
    Re: running a shell command from a python program

    Am Wed, 23 Feb 2005 07:00:31 -0800 schrieb Sandman:
    [color=blue]
    > Hi,
    > I'm a newbie, so please be gentle :-)
    >
    > How would I run a shell command in Python?[/color]
    [cut][color=blue]
    > Is popen the answer? Also, where online would I get access to good
    > sample code that I could peruse?[/color]

    Yes, popen is the answer. I recommend popen4 because it avoids
    deadlocks if there is output on stdout and stderr.

    Example:
    stdout, stdin = popen2.popen4(" tidy -q -errors '%s'" % htmlfile)

    HTH,
    Thomas

    --
    Thomas Güttler, http://www.thomas-guettler.de/


    Comment

    • Sandman

      #3
      Re: running a shell command from a python program

      Doh, use the search Luke:

      Running shell commands from Python - Programming in Python and Ruby - Programming in the Python and Ruby languages ; forum discussion


      Seems like popen is the way to go.

      S

      Comment

      • Leif B. Kristensen

        #4
        Re: running a shell command from a python program

        Sandman wrote:
        [color=blue]
        > How would I run a shell command in Python?
        >
        > Here is what I want to do:
        > I want to run a shell command that outputs some stuff, save it into a
        > list and do stuff with the contents of that list.[/color]

        There's a Python Cookbook example that should fit nicely with what
        you're trying to do here:

        <http://aspn.activestat e.com/ASPN/Cookbook/Python/Recipe/52296>
        --
        Leif Biberg Kristensen

        Comment

        • aurora

          #5
          Re: running a shell command from a python program

          In Python 2.4, use the new subprocess module for this. It subsume the
          popen* methods.
          [color=blue]
          > Hi,
          > I'm a newbie, so please be gentle :-)
          >
          > How would I run a shell command in Python?
          >
          > Here is what I want to do:
          > I want to run a shell command that outputs some stuff, save it into a
          > list and do stuff with the contents of that list.
          >
          > I started with a BASH script actually, until I realized I really needed
          > better data structures :-)
          >
          > Is popen the answer? Also, where online would I get access to good
          > sample code that I could peruse?
          >
          > I'm running 2.2.3 on Linux, and going strictly by online doc so far.
          >
          > Thanks!
          > S C
          >[/color]

          Comment

          Working...