How to use subprocess.Popen with built-in command on Windows
<p>In my old python script, I use the following code to show the result for Windows cmd command:</p>

<pre><code>print(os.popen("dir c:\\").read())
</code></pre>

<p>As the python 2.7 document said <code>os.popen</code> is obsolete and <code>subprocess</code> is recommended. I follow the documentation as:</p>

<pre><code>result = subprocess.Popen("dir c:\\").stdout
</code></pre>

<p>And I got error message:</p>

<pre><code>WindowsError: [Error 2] The system cannot find the file specified
</code></pre>

<p>Can you tell me the correct way to use the <code>subprocess</code> module?</p>

<p>You should use call <code>subprocess.Popen</code> with <code>shell=True</code> as below:</p>

<pre><code>import subprocess

result = subprocess.Popen("dir c:", shell=True,
                          stdout=subprocess.PIPE, stderr=subprocess.PIPE)

output,error = result.communicate()

print (output)
</code></pre>

<p><a href="https://pymotw.com/2/subprocess/" rel="nofollow">More info on subprocess module.</a></p>

python windows python-2.7 command-line subprocess