python subprocess sends backslash before a quote <p>I have a string, which is a framed command that should be executed by in command line</p> <p>cmdToExecute = "TRAPTOOL -a STRING "ABC" -o STRING 'XYZ'"</p> <p>I am considering the string to have the entire command that should be triggered from command prompt. If you take a closer look at the string cmdToExecute, you can see the option o with value XYZ enclosed in SINGLE QUOTE. There is a reason that this needs to be given in single quote orelse my tool TRAPTOOL will not be able to process the command.</p> <p>I am using subprocess.Popen to execute the entire command. Before executing my command in a shell, I am printing the content</p> <pre><code>print "Cmd to be exectued: %r" % cmdToExecute myProcess = subprocess.Popen(cmdToExecute, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False) (stdOut, stdErr) = myProcess.communicate() </code></pre> <p>The output of the above command is, Cmd to be executed: TRAPTOOL -a STRING "ABC" -o \'XYZ\'.</p> <p>You can see that the output shows a BACKWARD SLASH added automatically while printing. Actually, the \ is not there in the string, which I tested using a regex. But, when the script is run on my box, the TRAPTOOL truncates the part of the string XYZ on the receiving server. I manually copy pasted the print output and tried sending it, I saw the same error on the receiving server. However, when I removed the backward slash, it sent the trap without any truncation.</p> <ol> <li>Can anyone say why this happens?</li> <li>Is there anyway where we can see what command is actually executed in subprocess.Popen?</li> <li>Is there any other way I can execute my command other that subprocess.Popen that might solve this problem?</li> </ol>