title: Python 3 Escaping Quotes in System Calls
body: <p>I'm trying to convert new lines in a text file to "\n" characters in a string by running an Awk command using os.system:</p>
  
  <pre><code>awk '{printf "%s\\n", $0}' /tmp/file
  </code></pre>
  
  <p>My problem is that escaping the quotes is not working:</p>
  
  <pre><code>$ replace = "awk '{printf \"%s\\n\", $0}' /tmp/file"
  $ print(replace)
  awk '{printf "%s\n", $0}' /tmp/file
  </code></pre>
  
  <p>I've tried using shlex as well:</p>
  
  <pre><code>$ import shlex
  $ line = "awk '{printf \"%s\\n\", $0}'"
  $ lexer = shlex.shlex(line, posix=True)
  $ lexer.escapedquotes = "'\""
  $ mystring = ','.join(lexer)
  $ print(mystring)
  awk,{printf "%s\n", $0}
  </code></pre>
  
  <p>I either need to know how to run this awk command with Python or how to convert carriage returns/new lines in a file to "\n" in a string.</p>
  
  <p>EDIT: Here's an example file:</p>
  
  <pre><code>oneline
  twoline
  
  threeline
  </code></pre>
  
  <p>A method should return the following from this file:</p>
  
  <pre><code>oneline\ntwoline\n\nthreeline
  </code></pre>