Odd win32pipe.popen behavior

Mark Wright mwright at pro-ns.net
Fri May 18 22:12:06 CEST 2001


I've been trying to use win32pipe.popen with a command that has a
space and an argument that has a space.  This requires that the
arguments be quoted, like this (argv.py just prints sys.argv):

    "c:\program files\python\python.exe" argv.py "a b c d"

This works find from the command line:

    c:\>"c:\Program Files\Python\python.exe" argv.py "a b c d"
    ['argv.py', 'a b c d']

But in popen, it fails:

    c:\>python
    Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on
win32
    Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
    >>> from win32pipe import popen
    >>> popen(r'"c:\Program Files\Python\python.exe" argv.py "a b c
d"').read()
    ''

Unquoting the command fixes the problem:

    Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on
win32
    Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
    >>> from win32pipe import popen
    >>> popen(r'python.exe argv.py "a b c d"').read()
    "['argv.py', 'a b c d']\012"

Any idea why this is happening?  I can imagine work-arounds (add
'c:\program files\Python\python.exe' to the path for example), but
none are pretty.  I guess that this problem is related to the fact
that 'start' interprets the first quoted argument as a title for a new
command window (seriously! look at "start /?".  Yes, this is
absolutely insane.)  However, changing the command string to:

    r'"" "c:\Program Files\Python\python.exe" argv.py "a b c d"'

doesn't fix the problem.  Changing it to this does:

    r'start /b "" "c:\Program Files\Python\python.exe" argv.py "a b c
d"'

But this isn't a very impressive solution.  Is there a way to fix
this?
----
Mark
mwright at pro-ns.net



More information about the Python-list mailing list