subprocess and & (ampersand)
Tim Golden
mail at timgolden.me.uk
Wed Jan 23 06:12:57 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?
Although I'm sure you'll have looked into this already, the
subprocess module on Windows is using CreateProcess pretty
straightforwardly:
http://msdn2.microsoft.com/en-us/library/ms682425(VS.85).aspx
The docs there say that, to call a batch file, you need to
specify the command interpreter with /c and pass the batch
file, but as far as I can see it makes no difference! (Probably
means there's a special-caser behind the scenes of CreateProcess).
TJG
More information about the Python-list
mailing list