SaltyCrane: processeshttps://www.saltycrane.com/blog/2011-04-13T23:25:37-07:00How to use the bash shell with Python's subprocess module instead of /bin/sh
2011-04-13T23:25:37-07:00https://www.saltycrane.com/blog/2011/04/how-use-bash-shell-python-subprocess-instead-binsh/<p>By default, running <code>subprocess.Popen</code> with <code>shell=True</code>
uses <code>/bin/sh</code> as the shell. If you want to change the shell to
<code>/bin/bash</code>, set the <code>executable</code> keyword argument
to <code>/bin/bash</code>.
</p>
<p>Solution thanks this great article: <a href="http://jimmyg.org/blog/2009/working-with-python-subprocess.html">
Working with Python subprocess - Shells, Processes, Streams, Pipes, Redirects and More</a>
</p>
<pre class="python">import subprocess
def bash_command(cmd):
subprocess.Popen(cmd, shell=True, executable='/bin/bash')
bash_command('a="Apples and oranges" && echo "${a/oranges/grapes}"')</pre>
<p>Output:</p>
<pre>Apples and grapes</pre>
<p>For some reason, the above didn't work for my specific case, so I had to use the
following instead:</p>
<pre class="python">import subprocess
def bash_command(cmd):
subprocess.Popen(['/bin/bash', '-c', cmd])</pre>
<h4>See also</h4>
<ul>
<li><a href="http://docs.python.org/library/subprocess.html#subprocess.Popen">
Python <code>subprocess.Popen</code> documentation</a></li>
<li><a href="https://wiki.ubuntu.com/DashAsBinSh">
Ubuntu documentation on differences between dash and bash</a></li>
<li><a href="http://www.gnu.org/software/bash/manual/html_node/index.html">
GNU Bash Reference Manual</a></li>
</ul>
How to capture stdout in real-time with Python
2009-10-12T16:54:59-07:00https://www.saltycrane.com/blog/2009/10/how-capture-stdout-in-real-time-python/<p>This solution is thanks to <a href="http://blog.kagesenshi.org/2008/02/teeing-python-subprocesspopen-output.html">this article</a>.</p>
<pre class="python">import subprocess
def myrun(cmd):
"""from http://blog.kagesenshi.org/2008/02/teeing-python-subprocesspopen-output.html
"""
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout = []
while True:
line = p.stdout.readline()
stdout.append(line)
print line,
if line == '' and p.poll() != None:
break
return ''.join(stdout)</pre>
How to get stdout and stderr using Python's subprocess module
2008-09-23T12:48:00-07:00https://www.saltycrane.com/blog/2008/09/how-get-stdout-and-stderr-using-python-subprocess-module/<p>I wrote previously about
<a href="http://www.saltycrane.com/blog/2007/03/how-to-use-python-and-popen4-to-capture_12/">
how to get stdout and stderr using <code>os.popen4</code></a>.
However, per the Python documentation, using the
<code>subprocess</code> module is preferred:
<blockquote>The subprocess module allows you to spawn new processes,
connect to their input/output/error pipes, and obtain their return
codes. This module intends to replace several other, older modules
and functions, such as:<br>
<br>
os.system<br>
os.spawn*<br>
os.popen*<br>
popen2.*<br>
commands.*</blockquote>
</p>
<p>See the <a href="https://docs.python.org/2/library/subprocess.html">
subprocess module documentation</a> for more information.</p>
<p>Here is how to get stdout and stderr from a program using the
subprocess module:</p>
<pre class="python">from subprocess import Popen, PIPE, STDOUT
cmd = 'ls /etc/fstab /etc/non-existent-file'
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
output = p.stdout.read()
print output</pre>
<p>Results:</p>
<pre>ls: cannot access /etc/non-existent-file: No such file or directory
/etc/fstab
</pre>
How to use python and popen4 to capture stdout and stderr from a command
2007-03-12T15:10:00-07:00https://www.saltycrane.com/blog/2007/03/how-to-use-python-and-popen4-to-capture_12/<div><font face="Arial" color="#000080" size="2"><span style="FONT-SIZE: small; COLOR: black; FONT-FAMILY: arial">You can use popen to capture stdout from a command:<br /></span></font><pre>import os<br />stdout = os.popen("dir asdkfhqweiory")<br />print stdout.read()</pre>And your output will be something like:<br /><pre>>>> ================================ RESTART ================================<br /> >>> <br /> Volume in drive C has no label.<br /> Volume Serial Number is XXXXXXXX<br /> <br /> Directory of C:\Python25<br /> <br /> <br /> >>> </pre>If you wanted the error message, popen won't give it to you. To capture both stdout and stderr, use popen4:<br /><pre>import os<br /><br />(dummy, stdout_and_stderr) = os.popen4("dir asdkfhqweiory")<br />print stdout_and_stderr.read()</pre>This will give you the following output (which includes the error message):<br /><pre>>>> ================================ RESTART ================================<br /> >>> <br /> Volume in drive C has no label.<br /> Volume Serial Number is XXXXXXXX<br /> <br /> Directory of C:\Python25<br /> <br /> File Not Found<br /> <br /> >>> </pre>See <a href="http://docs.python.org/lib/os-newstreams.html">http://docs.python.org/lib/os-newstreams.html</a> for more information.</div>