popen eating quotes?

Discussion in 'Python' started by Bryan, Aug 3, 2003.

  1. Bryan

    Bryan Guest

    i have a batch file that contains these two lines:

    -- args.bat

    echo %1
    echo %2


    when i run args.bat from the command line it works correctly... notice that
    abc has quotes in the first example and not in the second.

    C:\Temp>"c:\program files\test\args" -vv abc
    C:\Temp>echo -vv
    -vv
    C:\Temp>echo abc
    abc

    C:\Temp>"c:\program files\test\args" -vv "abc"
    C:\Temp>echo -vv
    -vv
    C:\Temp>echo "abc"
    "abc"


    from python, i'm having trouble getting the quotes around abc to work

    >>> import os
    >>> def run_args(arg):

    .... i, o = os.popen4('"c:/program files/test/args.bat" -vv %s' % arg)
    .... print o.read()
    ....
    >>> run_args('abc')

    C:\python\python23\lib\site-packages>echo -vv
    -vv
    C:\python\python23\lib\site-packages>echo abc
    abc

    >>> run_args('"abc"')

    'c:/program' is not recognized as an internal or external command,
    operable program or batch file.


    what is going on here? why does "abc" effect the uoted program name. does
    anyone know how to fix this? how to pass a quoted arguments through to
    popen? i need to do this because in my real code, i'm passing paths as
    arguments and need them quoted.

    i'm using python 2.3 on winxp.

    thanks,

    bryan
     
    Bryan, Aug 3, 2003
    #1
    1. Advertising

  2. Bryan

    Jeff Epler Guest

    This has been discussed before. One such thread that I participated in
    was
    http://groups.google.com/groups?hl=...selm=0000161b%40bossar.com.pl&rnum=1&filter=0
    [apologies for the long URL, I don't know how to get a good memorable
    URL for a google groups search]

    This really is a cesspool on Windows, and the fault isn't Python's.
    Python is just calling the Windows functions, which insist on behaving
    badly compared to the Unix behavior.

    unix bigot'ly yours,
    jeff
     
    Jeff Epler, Aug 3, 2003
    #2
    1. Advertising

  3. Bryan

    Bryan Guest

    "Jeff Epler" <> wrote in message
    news:...
    > This has been discussed before. One such thread that I participated in
    > was
    >

    http://groups.google.com/groups?hl=...selm=0000161b%40bossar.com.pl&rnum=1&filter=0
    > [apologies for the long URL, I don't know how to get a good memorable
    > URL for a google groups search]
    >
    > This really is a cesspool on Windows, and the fault isn't Python's.
    > Python is just calling the Windows functions, which insist on behaving
    > badly compared to the Unix behavior.
    >
    > unix bigot'ly yours,
    > jeff
    >


    thanks for the link, but it didn't help me solve my problem. i just read
    many articles posted about popen and windows and there seems to be a
    solution to this problem. i need to pass a path to a program using popen so
    i can capture the output. since the path and/or file name can have spaces
    in it, i need to quote it. what's the way to get around this issue? i saw
    it mentioned in several places to try win32pipe.popen, but it had the same
    problem as os.popen. so, is this something that cannot be done in python at
    all with no work-around? is there another way besides using os.system or
    os.popen?

    >>> import os
    >>> def run_args(arg):

    .... i, o = os.popen4('"c:/program files/test/args.bat" -vv %s' % arg)
    .... print o.read()
    ....
    >>> run_args('"this is a very long path with spaces"')

    'c:/program' is not recognized as an internal or external command,
    operable program or batch file.

    thanks,

    bryan
     
    Bryan, Aug 4, 2003
    #3
  4. [OT] Google URLs (was Re: popen eating quotes?)

    Quoth Jeff Epler:
    > http://groups.google.com/groups?hl=...selm=0000161b%40bossar.com.pl&rnum=1&filter=0
    > [apologies for the long URL, I don't know how to get a good memorable
    > URL for a google groups search]


    Not memorable, but at least shorter, is
    <http://groups.google.com/groups?threadm=0000161b%40bossar.com.pl>
    In general all you need is the threadm or selm parameter, which
    gives the message-id of the post in question; if memory serves,
    threadm and selm differ in whether the resulting page shows the
    rest of the thread.

    (Ignore the selm in your URL above; it's inside the prev
    parameter, which stores information about where you were before
    arriving at the page the URL is actually for.)

    It seems that Google also assigns an id to each thread; with
    suitable poking around (see, e.g., the source for the page at the
    above URL) you can locate a 'th' parameter which can be used in
    place of the threadm/selm parameter if you wish to refer to the
    thread as a whole. In your case that's
    <http://groups.google.com/groups?th=77576209b6262476>
    (As this illustrates, the resulting URL is often shorter than one
    using the message-id.) The first ten messages in the thread
    appear on the page obtained thus, with anchors, so you can refer
    to them individually by appending '#link1', '#link2', etc. This
    makes nice short URLs too (though I'm not certain that *which*
    messages such anchors refer to is constant over time as the thread
    grows).

    All discovered empirically. Use at your own risk.

    --
    Steven Taschuk
    "Telekinesis would be worth patenting." -- James Gleick
     
    Steven Taschuk, Aug 4, 2003
    #4
  5. Bryan wrote in news:LOgXa.57808$o%2.28737@sccrnsc02:

    >>>> import os
    >>>> def run_args(arg):

    > ... i, o = os.popen4('"c:/program files/test/args.bat" -vv %s' % arg)
    > ... print o.read()
    > ...
    >>>> run_args('"this is a very long path with spaces"')

    > 'c:/program' is not recognized as an internal or external command,
    > operable program or batch file.
    >
    >


    I just got the following working where os.getcwd() is returning:
    'C:\\Documents and Settings\\Rob\\Desktop'


    >>> def f():

    .... i, o = os.popen4( "\"" + os.getcwd() + "\\aaa.bat\"" )
    .... print o.read()
    .... i.close()
    .... o.close()
    ....


    If this dosen't help, then (sound of large hammer being removed
    from a tool box) maybe this will:

    def g(arg):
    x = os.getcwd()
    os.chdir( "c:/program files/test/" )
    i, o = os.popen4( 'args.bat -vv %s' % arg )
    os.chdir( x )
    print o.read()


    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Aug 4, 2003
    #5
  6. Bryan

    Bryan Guest

    "Rob Williscroft" <> wrote in message
    news:Xns93CDD4975617ukcoREMOVEfreenetrtw@195.129.110.201...
    > Bryan wrote in news:LOgXa.57808$o%2.28737@sccrnsc02:
    >
    > >>>> import os
    > >>>> def run_args(arg):

    > > ... i, o = os.popen4('"c:/program files/test/args.bat" -vv %s' % arg)
    > > ... print o.read()
    > > ...
    > >>>> run_args('"this is a very long path with spaces"')

    > > 'c:/program' is not recognized as an internal or external command,
    > > operable program or batch file.
    > >
    > >

    >
    > I just got the following working where os.getcwd() is returning:
    > 'C:\\Documents and Settings\\Rob\\Desktop'
    >
    >
    > >>> def f():

    > ... i, o = os.popen4( "\"" + os.getcwd() + "\\aaa.bat\"" )
    > ... print o.read()
    > ... i.close()
    > ... o.close()
    > ...
    >
    >
    > If this dosen't help, then (sound of large hammer being removed
    > from a tool box) maybe this will:
    >
    > def g(arg):
    > x = os.getcwd()
    > os.chdir( "c:/program files/test/" )
    > i, o = os.popen4( 'args.bat -vv %s' % arg )
    > os.chdir( x )
    > print o.read()
    >
    >
    > Rob.
    > --
    > http://www.victim-prime.dsl.pipex.com/



    rob,

    thanks... this is exactly the solution i came up with too. it does remove
    the path, but not the spaces in the file name. i _might_ be able to live
    with this hack though.

    the code i'm porting to python is from perl, and the perl code doesn't have
    the popen issue.

    @xxxoutput=`xxx -vv "$file"`;

    now i won't be able show how cool python is :(

    bryan
     
    Bryan, Aug 4, 2003
    #6
  7. Bryan

    Ben Finney Guest

    Re: [OT] Google URLs (was Re: popen eating quotes?)

    On Sun, 3 Aug 2003 17:45:00 -0600, Steven Taschuk wrote:
    > Not memorable, but at least shorter, is
    > <http://groups.google.com/groups?threadm=0000161b%40bossar.com.pl>
    > In general all you need is the threadm or selm parameter, which
    > gives the message-id of the post in question; if memory serves,
    > threadm and selm differ in whether the resulting page shows the
    > rest of the thread.


    Fortunately, the Message-Id: is available in the message itself (it's
    part of every NNTP posting), so even without Google you theoretically
    have complete information to give the Google Groups URL for any post:

    <http://groups.google.com/groups?threadm=MESSAGE-ID>

    for threaded, or

    <http://groups.google.com/groups?selm=MESSAGE-ID>

    for the message alone.

    > All discovered empirically. Use at your own risk.


    Likewise.

    --
    \ Eccles: "I just saw the Earth through the clouds!" Lew: "Did |
    `\ it look round?" Eccles: "Yes, but I don't think it saw me." |
    _o__) -- The Goon Show, _Wings Over Dagenham_ |
    Ben Finney <http://bignose.squidly.org/>
     
    Ben Finney, Aug 4, 2003
    #7
  8. Bryan

    Miki Tebeka Guest

    Hello Bryan,

    > >>> run_args('"abc"')

    > 'c:/program' is not recognized as an internal or external command,
    > operable program or batch file.


    Didn't check but maybe win32api.GetShortPath will help.
    (Get Mark's excellent package from http://starship.python.net/crew/mhammond/)

    HTH.
    Miki
     
    Miki Tebeka, Aug 4, 2003
    #8
  9. Re: [OT] Google URLs (was Re: popen eating quotes?)

    On Sun, 3 Aug 2003 17:45:00 -0600, Steven Taschuk
    <> wrote:

    >Quoth Jeff Epler:
    >> http://groups.google.com/groups?hl=...selm=0000161b%40bossar.com.pl&rnum=1&filter=0
    >> [apologies for the long URL, I don't know how to get a good memorable
    >> URL for a google groups search]

    >
    >Not memorable, but at least shorter, is
    > <http://groups.google.com/groups?threadm=0000161b%40bossar.com.pl>
    >In general all you need is the threadm or selm parameter, which
    >gives the message-id of the post in question; if memory serves,
    >threadm and selm differ in whether the resulting page shows the
    >rest of the thread.
    >
    >(Ignore the selm in your URL above; it's inside the prev
    >parameter, which stores information about where you were before
    >arriving at the page the URL is actually for.)
    >
    >It seems that Google also assigns an id to each thread; with
    >suitable poking around (see, e.g., the source for the page at the
    >above URL) you can locate a 'th' parameter which can be used in
    >place of the threadm/selm parameter if you wish to refer to the
    >thread as a whole. In your case that's
    > <http://groups.google.com/groups?th=77576209b6262476>
    >(As this illustrates, the resulting URL is often shorter than one
    >using the message-id.) The first ten messages in the thread
    >appear on the page obtained thus, with anchors, so you can refer
    >to them individually by appending '#link1', '#link2', etc. This
    >makes nice short URLs too (though I'm not certain that *which*
    >messages such anchors refer to is constant over time as the thread
    >grows).
    >
    >All discovered empirically. Use at your own risk.


    If all you want is a short URL for posting, have a look at
    http://tinyurl.com/

    Jeff's *long* URL would then be: http://tinyurl.com/iy1q

    --Christopher
     
    Christopher Koppler, Aug 4, 2003
    #9
    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. Chris White

    Quotes/Double Quotes in Image Control

    Chris White, Sep 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,898
    Hermit Dave
    Sep 22, 2004
  2. Chris
    Replies:
    1
    Views:
    13,721
    Oisin
    Mar 24, 2006
  3. Lawrence Tierney

    Multiline quotes - escaping quotes - et al

    Lawrence Tierney, Dec 24, 2003, in forum: Java
    Replies:
    3
    Views:
    4,532
    Andrew Thompson
    Dec 24, 2003
  4. jOhn
    Replies:
    1
    Views:
    253
    Phlip
    Jan 29, 2008
  5. Richard Sandoval
    Replies:
    5
    Views:
    216
    7stud --
    Apr 26, 2011
Loading...

Share This Page