subprocess and & (ampersand)
Tim Golden
mail at timgolden.me.uk
Wed Jan 23 06:06:34 EST 2008
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?
A little experimentation suggests that the problem's somehow
tied up with the .bat file. ie this works for me (doubly
complicated because of the long firefox path:
<code>
import subprocess
cmd = [
r"c:\Program Files\Mozilla Firefox\firefox.exe",
"http://local.goodtoread.org/search?word=tim&cached=0"
]
subprocess.Popen (cmd)
</code>
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",
"http://local.goodtoread.org/search?word=tim&cached=0"
]
subprocess.Popen (cmd)
</code>
although, interestingly, it seems to cut off at the
"=" before the "&". Not sure how significant that is.
So, even assuming we're looking at the same situation,
I suppose one solution for you is to break out the
.bat file. But that may not be a possibility.
TJG
More information about the Python-list
mailing list