subprocess has PIPE build in.
Because of security reasons.
So can look at a command like this
It will search for hello in foo.txt,split at
shell=False
is default,and do not use shell=True
so can pass in the whole shell pipe command.Because of security reasons.
So can look at a command like this
grep -o "hello" foo.txt | wc -l
It will search for hello in foo.txt,split at
|
.stdout=subprocess.PIPE
which tells subprocess to send that output to the respective file handler.import subprocess # grep -o "hello" foo.txt | wc -l grep_proc = subprocess.Popen(['grep', '-o', 'hello', 'foo.txt'],stdout=subprocess.PIPE) wc_proc = subprocess.Popen(['wc', '-l'],stdin=grep_proc.stdout, stdout=subprocess.PIPE) result, arg_1 = wc_proc.communicate() print(result.decode())