This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: problems errors when calling cmd.exe
Type: behavior Stage: test needed
Components: Windows Versions: Python 2.6
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: tim.golden Nosy List: ajaksu2, collinwinter, ggenellina, tim.golden, tzellman
Priority: normal Keywords:

Created on 2007-05-07 17:13 by tzellman, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (7)
msg31976 - (view) Author: tzellman (tzellman) Date: 2007-05-07 17:13
An example:

>>> import subprocess
>>> proc = subprocess.Popen('"c:\Windows\system\ping.exe" "localhost"', shell=1, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

'c:\Windows\system32\ping.exe" "localhost' is not recognized as an internal or external command, operable program or batch file.

This normally works from the cmd.exe prompt, so the command line is being passed incorrectly in the subprocess module.

My ACTUAL use case arises when I want to call the Microsoft compiler (CL.exe), and add include directories that have spaces in the name. For example:

"C:\Program Files\Microsoft Visual Studio 8\VC\BIN\CL.exe" /TC /O2 /DNDEBUG /W3
/nologo /c /EHsc /errorReport:prompt /Idefault\ /I.. /I. /Idefault /I"C:\Program Files\GnuWin32" /DWIN32 ..\pcreposix.c /Fodefault\pcreposix.obj

The fix for this problem is to modify line 765 in, replacing it with:

args = comspec + " /s /c \"%s\"" % args.replace('""', '"')

The /s tells cmd.exe not to re-format the command line. We then encase the entire command line in double quotes. I also replace occurrences of "" with " in the arg string, in case the user already encased the command in double quotes.

With this fix, the commands execute correctly.
msg31977 - (view) Author: tzellman (tzellman) Date: 2007-05-07 17:20
This could likely be rejected.

I realize a solution to this w/o modifying the Python source is to encase the expression yourself in double quotes. So, the example given would look like:
>>> import subprocess
>>> proc = subprocess.Popen('""c:\Windows\system\ping.exe" "localhost""',
shell=1, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

And that will work.

Sorry for any confusion.
msg31978 - (view) Author: Gabriel Genellina (ggenellina) Date: 2007-05-07 21:06
Use a list of arguments instead, and please remember to use raw strings or double backslashes:

proc = subprocess.Popen([r"c:\Windows\system\ping.exe","localhost"],
shell=1, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
msg31979 - (view) Author: Collin Winter (collinwinter) * (Python committer) Date: 2007-06-05 18:36
tzellman, could you work up a real patch and a test case for this? Thanks.
msg84722 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-03-30 23:54
Collin: do you still want a patch or is this a won't fix?
msg84749 - (view) Author: Collin Winter (collinwinter) * (Python committer) Date: 2009-03-31 04:04
Do you know of anyone actively working on Windows support? If not, I say
close it as "won't fix".
msg113281 - (view) Author: Tim Golden (tim.golden) * (Python committer) Date: 2010-08-08 16:42
Duplicate of #2304
Date User Action Args
2022-04-11 14:56:24adminsetgithub: 44941
2010-08-08 16:42:28tim.goldensetstatus: open -> closed
resolution: duplicate
messages: + msg113281
2010-08-06 15:10:52tim.goldensetassignee: tim.golden

nosy: + tim.golden
2009-03-31 04:04:06collinwintersetmessages: + msg84749
2009-03-30 23:54:47ajaksu2setversions: + Python 2.6, - Python 2.5
nosy: + ajaksu2

messages: + msg84722

type: behavior
stage: test needed
2007-05-07 17:13:43tzellmancreate