title: python bash command: how to escape single quote?
body: <p>I'm trying to execute a shell command through python. The command is like the following one: </p>
  
  <pre><code>su -c "lftp -c 'open -u user,password ftp://127.0.0.1; get ivan\'s\ filename.pdf' " someuser
  </code></pre>
  
  <p>So, when I try to do it in python: </p>
  
  <pre><code>command = "su -c \"lftp -c 'open -u user,password ftp://127.0.0.1; get ivan\'s\ filename.pdf' \" someuser"
  os.system(command)
  </code></pre>
  
  <p>Or: </p>
  
  <pre><code>command = subprocess.Popen(["su", "-c", "lftp -c 'open -u user,password ftp://127.0.0.1; get ivan\'s\ filename.pdf'", "someuser"])
  </code></pre>
  
  <p>I get the following error: </p>
  
  <pre><code>bash: -c: line 0: unexpected EOF while looking for matching `''
  bash: -c: line 1: syntax error: unexpected end of file
  </code></pre>
  
  <p>Referred to: ivan\'s single quote. </p>
  
  <p>I know there are a lot of single/double quotes in that but how can I escape this? </p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>EDIT: THIS WORKED FOR ME:</strong> </p>
  
  <pre><code>subprocess.call(["su","-c",r"""lftp -c "open -u user,password ftp://127.0.0.1; get ivan\'s\ filename.pdf" """, "someuser"])
  </code></pre>
  
  <p>Thank you all very much!</p>