By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,153 Members | 960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,153 IT Pros & Developers. It's quick & easy.

subprocess and & (ampersand)

P: n/a
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?

Thanks,

STeVe
Jan 23 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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/arc.../shellscr.mspx

http://technet2.microsoft.com/Window...a166f1033.mspx
Hope this helps,

--
Steven
Jan 23 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.