subprocess and & (ampersand)

Steven Bethard steven.bethard at
Wed Jan 23 17:11:18 CET 2008

Ross Ridge wrote:
> Tim Golden  <mail at> wrote:
>> but this doesn't:
>> <c:/temp/firefox.bat>
>> "c:\Program Files\Mozilla Firefox\firefox.exe" "%*"
>> </c:/temp/firefox.bat>
>> <code>
>> import subprocess
>> cmd = [
>> r"c:\temp\firefox.bat",
>> ""
>> ]
>> subprocess.Popen (cmd)
>> </code>
> Ross Ridge wrote:
>> You need to use double quotes both in the .BAT file and in the string
>> you pass to subprocess.Popen().
> Tim Golden  <mail at> wrote:
>> ... If you simply requote the second element in the cmd list
>> ('"http:/....."') then the internal quotes are escaped by some part of
>> the mechanism and it still doesn't work.
> Hmm... I guess things are much more messy than that.  CMD doesn't do
> standard quote processing of it's arguments or .BAT file arguments, and
> so is incompatible with how subprocess quotes args lists.  It looks like
> you need use a string instead of list with subprocess.Popen and not use
> quotes in the batch file.  So something like:
> firefox.bat:
> 	"c:\Program Files\Mozilla Firefox\firefox.exe" %1
> code:
> 	subprocess.Popen(r'c:\temp\firefox.bat ""')

This works.  The other thing that works is to put extra quotes around 
the URL when you pass in the list::

   >>> command= 'lynx.bat', '-dump', '"^&y=2"'
   >>> proc = subprocess.Popen(command,
   ...                         stdin=subprocess.PIPE,
   ...                         stdout=subprocess.PIPE,
   ...                         stderr=subprocess.PIPE)
   '   You have reached this web page by typing "",
   "",\n   or "" into your web browser.\n\n   These
   domain  names are reserved for use in documentation and are not\n
   available for registration. See [1]RFC 2606, Section
   3.\n\nReferences\n\n   1.\n'

I tried this before, and posted it, but I had mistakenly read from 
stderr instead of stdout. Thanks for making me double-check that code!


More information about the Python-list mailing list