Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell piping with subprocess in Python

I read every thread I found on StackOverflow on invoking shell commands from Python using subprocess, but I couldn't find an answer that applies to my situation below:

I would like to do the following from Python:

  1. Run shell command command_1. Collect the output in variable result_1

  2. Shell pipe result_1 into command_2 and collect the output on result_2. In other words, run command_1 | command_2 using the result that I obtained when running command_1 in the step before

  3. Do the same piping result_1 into a third command command_3 and collecting the result in result_3.

So far I have tried:

p = subprocess.Popen(command_1, stdout=subprocess.PIPE, shell=True)

result_1 = p.stdout.read();

p = subprocess.Popen("echo " + result_1 + ' | ' + 
command_2, stdout=subprocess.PIPE, shell=True)

result_2 = p.stdout.read();

the reason seems to be that "echo " + result_1 does not simulate the process of obtaining the output of a command for piping.

Is this at all possible using subprocess? If so, how?

like image 309
Amelio Vazquez-Reina Avatar asked Mar 03 '12 00:03

Amelio Vazquez-Reina


People also ask

How do you pass a pipe in subprocess Python?

To use a pipe with the subprocess module, you have to pass shell=True . In your particular case, however, the simple solution is to call subprocess. check_output(('ps', '-A')) and then str. find on the output.

How do I run a shell command in Python using subprocess?

Python Subprocess Run Function The subprocess. run() function was added in Python 3.5 and it is recommended to use the run() function to execute the shell commands in the python program. The args argument in the subprocess. run() function takes the shell command and returns an object of CompletedProcess in Python.

What is shell true in subprocess Python?

After reading the docs, I came to know that shell=True means executing the code through the shell. So that means in absence, the process is directly started.

What shell does Python subprocess use?

By default, running subprocess. Popen with shell=True uses /bin/sh as the shell. If you want to change the shell to /bin/bash , set the executable keyword argument to /bin/bash .


1 Answers

You can do:

pipe = Popen(command_2, shell=True, stdin=PIPE, stdout=PIPE)
pipe.stdin.write(result_1)
pipe.communicate()

instead of the line with the pipe.

like image 120
Daniel Avatar answered Nov 10 '22 00:11

Daniel