title: Passing arguments which include double quotes to subprocess using Python
body: <p>I'm looking for a way to pass an argument which contains double quotes to python's subprocess module, without the double quotes being escaped.</p>
  
  <p>I've seen this question asked a few different times when the quotes are surrounding the arg, but the answer is usually to remove the quotes. The issue I've hit is that the quotes are part of the argument and must be passed to the script without prepending a back slash to each quote</p>
  
  <p>example:</p>
  
  <p>I'm calling subprocesTest.bat, a simple batch script that takes an argument and echos it:</p>
  
  <pre><code>echo "%~1"
  </code></pre>
  
  <p>I invoke the batch script from python with an arg that includes quotes:</p>
  
  <pre><code>import subprocess
  test_string = 'Bill said "Hi!"'
  cmd = ["subprocesTest.bat",
         test_string
         ]
  
  p = subprocess.Popen(cmd)
  p.wait()
  </code></pre>
  
  <p>expected output:</p>
  
  <blockquote>
    <p>"Bill said "Hi!""</p>
  </blockquote>
  
  <p>actual output:</p>
  
  <blockquote>
    <p>"Bill said \"Hi!\""</p>
  </blockquote>
  
  <p>I can get the expected output when calling the same batch script from the command line as follows:</p>
  
  <blockquote>
    <p>subprocesTest.bat "Bill said "Hi!""</p>
  </blockquote>
  
  <p>Looking at the source, subprocess.py's list2cmdline function is documented with:</p>
  
  <pre><code> 3) A double quotation mark preceded by a backslash is
     interpreted as a literal double quotation mark.
  </code></pre>
  
  <p>However, if I step through this function, I see no means of passing a double quote through without prepending a backslash:</p>
  
  <pre><code>        elif c == '"':
              # Double backslashes.
              result.append('\\' * len(bs_buf)*2)
              bs_buf = []
              result.append('\\"')
  </code></pre>
  
  <p>So I have two questions...</p>
  
  <ul>
  <li>Is it possible to pass an arg to subprocess which contains a double
  quote wihtout prepending a backslash</li>
  <li>If it is not possible, can someone explain to me why the function
  was designed this way? Item three in the funcion's doc string
  seems to indicate that I can pass a literal quote by prepending a
  backslash, but this doesn't yield the behavior I was looking for.
  Am I reading the doc wrong?</li>
  </ul>
  
  <p>Here are other threads where this question is asked, and worked around by not using quotes:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/16224868/passing-double-quotation-marks-to-a-command-from-python-using-the-subprocess-mod">Passing double quotation marks to a command from Python using the subprocess module</a></li>
  <li><a href="https://stackoverflow.com/questions/14632356/how-can-i-tell-subprocess-to-stop-escaping-my-quotes">How can I tell subprocess to stop escaping my quotes?</a></li>
  </ul>
  
  <p>I see no response for a situation where I actually want to pass quotes in my argument</p>