Retrieving the output of subprocess.call() [duplicate]

If you have Python version >= 2.7, you can use subprocess.check_output which basically does exactly what you want (it returns standard output as string). Simple example (linux version, see note): import subprocess print subprocess.check_output([“ping”, “-c”, “1”, “8.8.8.8”]) Note that the ping command is using linux notation (-c for count). If you try this on Windows … Read more

How can I pipe stderr, and not stdout?

First redirect stderr to stdout — the pipe; then redirect stdout to /dev/null (without changing where stderr is going): command 2>&1 >/dev/null | grep ‘something’ For the details of I/O redirection in all its variety, see the chapter on Redirections in the Bash reference manual. Note that the sequence of I/O redirections is interpreted left-to-right, … Read more

How to use `subprocess` command with pipes

To use a pipe with the subprocess module, you have to pass shell=True. However, this isn’t really advisable for various reasons, not least of which is security. Instead, create the ps and grep processes separately, and pipe the output from one into the other, like so: ps = subprocess.Popen((‘ps’, ‘-A’), stdout=subprocess.PIPE) output = subprocess.check_output((‘grep’, ‘process_name’), … Read more

How do I use subprocess.Popen to connect multiple processes by pipes?

You’d be a little happier with the following. import subprocess awk_sort = subprocess.Popen( “awk -f script.awk | sort > outfile.txt”, stdin=subprocess.PIPE, shell=True ) awk_sort.communicate( b”input data\n” ) Delegate part of the work to the shell. Let it connect two processes with a pipeline. You’d be a lot happier rewriting ‘script.awk’ into Python, eliminating awk and … Read more