python Popen shell=True behavior
<p>Why would this doesn't work: <code>subprocess.Popen(["ls -l | grep myfile"], shell=False)</code>
But this line works: <code>subprocess.Popen(["ls -l | grep myfile"], shell=True)</code>
I understand this shell=True create a sub shell internally and execute command. But didn't understand how this affect Popen behavior</p>

<p>Popen() does the subprocess management with the code inside the subprocess module. It doesn't know about piping with <code>|</code>, and it doesn't know whether the string you pass is a program or an argument, like in your example with <code>ls</code> it will assume that everything else except <code>ls</code> itself is arguments for the program. It will try to execute the first item in list and pass all other items as arguments.</p>

<p>When you use <code>shell=True</code>, you may think of it (e.g. on UNIX) as running <code>/bin/sh -c</code> with arglist you provided as a string. So</p>

<pre><code>Popen('ls -l | grep myfile', shell=True)
</code></pre>

<p>is something close to</p>

<pre><code>Popen(['/bin/sh', '-c', 'ls -l | grep myfile'])
</code></pre>

<p>In both cases, argument handling is actually done by your shell.</p>

<p>For piping and <code>shell=False</code>, you should use <code>subprocess.PIPE</code> and stdout/stdin/stderr redirections with the tools provided in subprocess module. </p>

python python-3.x subprocess