Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - how to execute shell commands with pipe, but without 'shell=True'?

I have a case to want to execute the following shell command in Python and get the output,

echo This_is_a_testing | grep -c test 

I could use this python code to execute the above shell command in python,

>>> import subprocess >>> subprocess.check_output("echo This_is_a_testing | grep -c test", shell=True) '1\n' 

However, as I do not want to use the "shell=True" option, I tried the following python code,

>>> import subprocess >>> p1 = subprocess.Popen(["echo", "This_is_a_testing"], stdout=subprocess.PIPE) >>> p2 = subprocess.Popen(["grep", "-c", "test"], stdin=p1.stdout) >>> p1.stdout.close() >>> p2.communicate() (None, None) 

I wonder why the output is "None" as I have referred to the descriptions in the webpage : http://docs.python.org/library/subprocess.html#subprocess.PIPE

Had I missed some points in my code ? Any suggestion / idea ? Thanks in advance.

like image 513
user1129812 Avatar asked Feb 22 '12 10:02

user1129812


People also ask

Can I use python to execute shell commands?

Python allows you to execute shell commands, which you can use to start other programs or better manage shell scripts that you use for automation. Depending on our use case, we can use os. system() , subprocess. run() or subprocess.

How do you use the pipe command in python?

pipe() method in Python is used to create a pipe. A pipe is a method to pass information from one process to another process.

Can pipe module be used to run shell commands in a program?

pipe module can be used to run shell commands in a program Code Example.

What is subprocess Check_output in python?

The subprocess. check_output() is used to get the output of the calling program in python. It has 5 arguments; args, stdin, stderr, shell, universal_newlines. The args argument holds the commands that are to be passed as a string.


1 Answers

Please look here:

>>> import subprocess >>> p1 = subprocess.Popen(["echo", "This_is_a_testing"], stdout=subprocess.PIPE) >>> p2 = subprocess.Popen(["grep", "-c", "test"], stdin=p1.stdout) >>> 1 p1.stdout.close() >>> p2.communicate() (None, None) >>> 

here you get 1 as output after you write p2 = subprocess.Popen(["grep", "-c", "test"], stdin=p1.stdout), Do not ignore this output in the context of your question.

If this is what you want, then pass stdout=subprocess.PIPE as argument to the second Popen:

>>> p1 = subprocess.Popen(["echo", "This_is_a_testing"], stdout=subprocess.PIPE) >>> p2 = subprocess.Popen(["grep", "test"], stdin=p1.stdout, stdout=subprocess.PIPE) >>> p2.communicate() ('This_is_a_testing\n', None) >>> 
like image 186
avasal Avatar answered Sep 19 '22 06:09

avasal