subprocess and & (ampersand)

Steven Bethard steven.bethard at gmail.com
Wed Jan 23 02:03:31 EST 2008


Steven D'Aprano wrote:
> On Tue, 22 Jan 2008 22:53:20 -0700, Steven Bethard wrote:
> 
>> I'm having trouble using the subprocess module on Windows when my
>> command line includes special characters like "&" (ampersand)::
>>
>>  >>> command = 'lynx.bat', '-dump', 'http://www.example.com/?x=1&y=2'
>>  >>> kwargs = dict(stdin=subprocess.PIPE,
>> ...               stdout=subprocess.PIPE, ...              
>> stderr=subprocess.PIPE)
>>  >>> proc = subprocess.Popen(command, **kwargs) proc.stderr.read()
>> "'y' is not recognized as an internal or external command,\r\noperable
>> program or batch file.\r\n"
>>
>> As you can see, Windows is interpreting that "&" as separating two
>> commands, instead of being part of the single argument as I intend it to
>> be above.  Is there any workaround for this?  How do I get "&" treated
>> like a regular character using the subprocess module?
> 
> 
> That's nothing to do with the subprocess module. As you say, it is 
> Windows interpreting the ampersand as a special character, so you need to 
> escape the character to the Windows shell.
> 
> Under Windows, the escape character is ^, or you can put the string in 
> double quotes:
> 
> # untested
> command = 'lynx.bat -dump http://www.example.com/?x=1^&y=2'
> command = 'lynx.bat -dump "http://www.example.com/?x=1&y=2"'

Sorry, I should have mentioned that I already tried that. You get the 
same result::

   >>> command = 'lynx.bat', '-dump', 'http://www.example.com/?x=1^&y=2'
   >>> proc = subprocess.Popen(command,
   ...                         stdin=subprocess.PIPE,
   ...                         stdout=subprocess.PIPE,
   ...                         stderr=subprocess.PIPE)
   >>> proc.stderr.read()
   "'y' is not recognized as an internal or external command,\r\noperable
   program or batch file.\r\n"

In fact, the "^" doesn't seem to work at the command line either::

   >lynx.bat -dump http://www.example.com/?x=1^&y=2

   Can't Access `file://localhost/C:/PROGRA~1/lynx/1'
   Alert!: Unable to access document.

   lynx: Can't access startfile
   'y' is not recognized as an internal or external command,
   operable program or batch file.

Using quotes does work at the command line::

   C:\PROGRA~1\lynx>lynx.bat -dump "http://www.example.com/?x=1&y=2"
      You have reached this web page by typing "example.com",
      "example.net", or "example.org" into your web browser.

      These  domain  names are reserved for use in documentation and are
      not available for registration. See [1]RFC 2606, Section 3.

   References

      1. http://www.rfc-editor.org/rfc/rfc2606.txt

But I get no output at all when using quotes with subprocess::

   >>> command= 'lynx.bat', '-dump', '"http://www.example.com/?x=1^&y=2"'
   >>> proc = subprocess.Popen(command,
   ...                         stdin=subprocess.PIPE,
   ...                         stdout=subprocess.PIPE,
   ...                         stderr=subprocess.PIPE)
   >>> proc.stderr.read()
   ''

Any other ideas?

STeVe



More information about the Python-list mailing list