title: How to use Subprocess in Python
body: <p>I would like to execute following shell command in python: <code>grep 'string' file | tail -1 | cut -c 1-3</code></p>
  
  <p>I tried:</p>
  
  <pre><code>import subprocess
  
  i = 1
  while i &lt; 1070:
      file = "sorted." + str(i) + ".txt"
      string = "2x"
      subprocess.call(grep 'string' file | tail -1 | cut -c 1-3)
      i = i + 1
  </code></pre>
  
  <p>Any help would be appreciated. Thanks.</p>