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: subprocess popen arguments using double quotes
Type: behavior Stage:
Components: Versions: Python 2.7
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: paul.moore, rengine
Priority: normal Keywords:

Created on 2015-04-03 19:04 by rengine, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg240026 - (view) Author: rengine (rengine) Date: 2015-04-03 19:04
Using Python 2.7.9

Noticed when I run a subprocess Popen with an argument containing double quotes, it will have a different affect depending on my operating system. 

In Linux, if I run ./, it will call which will append someargs.txt correctly without escaped quotations

In Windows, if I run ./, it will call runme.bat which will append someargs.txt with escaped quotations

Also in Windows, if I run runme.bat with an argument containing quotations it will append someargs.txt correctly without escaped quotations so this problem seems to be stemming from Python.


import sys
import subprocess
import shlex

# works on Linux:
#command_line = "./ --include=\"check\""

# fails on Windows:
command_line = "runme.bat --include=\"check\""
#command_line = "runme.bat --include=\"check\""

arg = shlex.shlex(command_line)
arg.quotes = '"'
arg.whitespace_split = True
arg.commenters = ''
command_line_args = list(arg)
print command_line_args

command_line_process = subprocess.Popen(

line = ""
while True:
    line = command_line_process.stdout.readline()
    if line:
        print line

===================================== runme.bat

echo %*
echo %* >> someargs.txt


echo $@
echo $@ >> someargs.txt
msg240042 - (view) Author: Paul Moore (paul.moore) * (Python committer) Date: 2015-04-03 22:30
On Windows, subprocess.Popen has to convert an argument list to a command line (because that's what the OS expects for CreateProcess). It does so internally, using subprocess.list2cmdline, which follows the quoting rules for the MS C runtime (because that's what 99% of programs use). Unfortunately batfile argument parsing does *not* use these conventions, and you can find odd corner cases like this.

Specifically, it's not possible on Windows to "correctly" quote all forms of argument without knowing what program you are calling (because programs do their own argument parsing, usually but not always via the C runtime). The subprocess module has to work for unknown executables, so it chooses to do the right thing for programs using the MS C runtime argument parser (which is most programs).
Date User Action Args
2022-04-11 14:58:15adminsetgithub: 68050
2015-04-03 22:30:37paul.mooresetstatus: open -> closed

nosy: + paul.moore
messages: + msg240042

resolution: not a bug
2015-04-03 19:04:53renginecreate