subprocess and & (ampersand)
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Wed Jan 23 01:38:30 EST 2008
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"'
In Linux land, you would use a backslash or quotes.
To find the answer to this question, I googled for "windows how to escape
special characters shell" and found these two pages:
http://www.microsoft.com/technet/archive/winntas/deploy/prodspecs/shellscr.mspx
http://technet2.microsoft.com/WindowsServer/en/library/44500063-fdaf-4e4f-8dac-476c497a166f1033.mspx
Hope this helps,
--
Steven
More information about the Python-list
mailing list