problem with single-quote and double-quote when using subprocess.

Discussion in 'Python' started by Evan, Nov 3, 2008.

  1. Evan

    Evan Guest

    Hello -

    i'm trying to call subprocess.popen on the 'command-based' function in
    linux. When I run the command from the shell, like so:

    goset -f ' "%s %s" name addr ' file_name

    it works fine

    however when I try to do it in python like so:

    p = subprocess.Popen(["goest",'-f \'\"%s %s\" name addr\' ',
    'file_name'], shell=True)

    It always failed.

    I also try like so:

    p = subprocess.Popen(["goest","-f '\"%s %s\" name addr' ",
    'file_name'], shell=True)

    It also failed.

    Does anybody have a good suggestion for this matter? thanks in
    advance.

    Evan
     
    Evan, Nov 3, 2008
    #1
    1. Advertising

  2. Evan

    alex23 Guest

    On Nov 4, 8:47 am, Evan <> wrote:
    > It always failed.
    > It also failed.
    > Does anybody have a good suggestion for this matter? thanks in
    > advance.


    What do you mean by "it failed"? It always helps to paste the actual
    error message you received.
     
    alex23, Nov 4, 2008
    #2
    1. Advertising

  3. Evan

    MRAB Guest

    On Nov 3, 10:47 pm, Evan <> wrote:
    > Hello -
    >
    > i'm trying to call subprocess.popen on the 'command-based' function in
    > linux.  When I run the command from the shell, like so:
    >
    > goset -f ' "%s %s" name addr ' file_name
    >
    > it works fine
    >
    > however when I try to do it in python like so:
    >
    > p = subprocess.Popen(["goest",'-f \'\"%s %s\" name addr\' ',
    > 'file_name'], shell=True)
    >
    > It always failed.
    >
    > I also try like so:
    >
    > p = subprocess.Popen(["goest","-f '\"%s %s\" name addr' ",
    > 'file_name'], shell=True)
    >
    > It also failed.
    >
    > Does anybody have a good suggestion for this matter? thanks in
    > advance.
    >

    It looks like there are _4_ items on the command line:

    goset
    -f
    ' "%s %s" name addr '
    file_name

    so the call should be:

    p = subprocess.Popen(["goest", "-f", "' \"%s %s\" name addr '",
    "file_name"], shell=True)

    (Untested)
     
    MRAB, Nov 4, 2008
    #3
  4. Re: problem with single-quote and double-quote when usingsubprocess.

    On Tue, 04 Nov 2008 03:26:21 -0800, MRAB wrote:

    > On Nov 3, 10:47 pm, Evan <> wrote:
    >> i'm trying to call subprocess.popen on the 'command-based' function in
    >> linux.  When I run the command from the shell, like so:
    >>
    >> goset -f ' "%s %s" name addr ' file_name
    >>
    >> it works fine

    >
    > It looks like there are _4_ items on the command line:
    >
    > goset
    > -f
    > ' "%s %s" name addr '
    > file_name
    >
    > so the call should be:
    >
    > p = subprocess.Popen(["goest", "-f", "' \"%s %s\" name addr '",
    > "file_name"], shell=True)


    The argument after '-f' doesn't have the single quotes at both ends.
    They tell the shell that it is just one argument and the shell removes
    them before calling ``goset`` (or ``goest``).

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Nov 4, 2008
    #4
    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. Sydex
    Replies:
    12
    Views:
    6,602
    Victor Bazarov
    Feb 17, 2005
  2. Replies:
    3
    Views:
    13,387
  3. Eric Layman
    Replies:
    3
    Views:
    653
    Rad [Visual C# MVP]
    Apr 14, 2007
  4. Evan
    Replies:
    1
    Views:
    415
    Marc 'BlackJack' Rintsch
    Nov 4, 2008
  5. vikrant
    Replies:
    8
    Views:
    387
    vikrant
    May 17, 2007
Loading...

Share This Page