Re: subprocess and & (ampersand)



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
.



Relevant Pages

  • Re: Problem when executing application from batch file
    ... My batch file has 4 lines. ... > Apparently this is only supported in XP and most likely Windows 2000. ... >>> returned immediately before they finished executing. ...
    (microsoft.public.vb.general.discussion)
  • Re: Automated script to backup IE favorites?
    ... Diego - disregard last message I got it! ... If you don't want to use a batch file, ... you have to create a GPO to run the batch or WSH script on user logoff ... MS MVP Windows - Internet Explorer ...
    (microsoft.public.windows.inetexplorer.ie6.browser)
  • Re: digital pictures: reordering and renaming (in a different way)
    ... While searching google no how to use the batch mode in picasa, I discovered that Windows XP also has the feature to do this. ... this is a surprise for me actually that this dragging and dropping files to order them and then to rename them in a batch mode was more easily accomplised in Windows than in Linux. ... I have discovered that there is krename in KDE which can rename selected files. ...
    (Debian-User)
  • Re: batch file halts
    ... MS-MVP Windows Shell/User ... Anyway the only problem I have is that a batch file is halting ... If I execute each line in the batch file interactively at the command ...
    (microsoft.public.win2000.cmdprompt.admin)
  • Re: digital pictures: reordering and renaming (in a different way)
    ... While searching google no how to use the batch mode in picasa, I discovered that Windows XP also has the feature to do this. ... the first selected image file is not named in the same format as the others in the selection. ...
    (Debian-User)