Using "subprocess" without lists. . .?

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

    #1

    Using "subprocess" without lists. . .?

    Hi All,

    I've recently seen the "subprocess " module and am rather confused by
    it's requirements. Is it not possible to execute an entire string
    without having to break them up into a list of arguments? For
    instance, I'd much rather do the following:


    subprocess.call ("ls -al | grep -i test")


    .. . .than to have to:


    list = ["ls", "-a", "-l" ] . . . . . . and so on and so forth.
    subprocess.call (list. . .)


    What is the best way to go about executing a massively complex single
    line command?


    Thanks,
    Michael
  • Steven Bethard

    #2
    Re: Using "subproces s" without lists. . .?

    Michael Williams wrote:
    Hi All,
    >
    I've recently seen the "subprocess " module and am rather confused by
    it's requirements. Is it not possible to execute an entire string
    without having to break them up into a list of arguments? For instance,
    I'd much rather do the following:
    >
    >
    subprocess.call ("ls -al | grep -i test")
    >
    >
    . . .than to have to:
    >
    >
    list = ["ls", "-a", "-l" ] . . . . . . and so on and so forth.
    subprocess.call (list. . .)
    >
    >
    What is the best way to go about executing a massively complex single
    line command?

    You could always call "ls -al | grep -i test".split().

    STeVe

    Comment

    • Peter Otten

      #3
      Re: Using "subproces s" without lists. . .?

      Michael Williams wrote:
      I've recently seen the "subprocess " module and am rather confused by
      it's requirements. Is it not possible to execute an entire string
      without having to break them up into a list of arguments? For
      instance, I'd much rather do the following:
      >
      >
      subprocess.call ("ls -al | grep -i test")
      Try

      subprocess.call ("ls -al | grep -i test", shell=True)
      >
      >
      . . .than to have to:
      >
      >
      list = ["ls", "-a", "-l" ] . . . . . . and so on and so forth.
      subprocess.call (list. . .)
      which avoids a lot of problems with shell quoting.

      Peter


      Comment

      • Peter Otten

        #4
        Re: Using "subproces s" without lists. . .?

        Steven Bethard wrote:
        You could always call "ls -al | grep -i test".split().
        Or better, shlex.split().

        Peter

        Comment

        Working...