HOw can I pipe in result of which somecommand
to outerCommand
so as to replicate result of what I will do in shell as outercommand 'which somecommand'
Asked
Active
Viewed 113 times
0

Chang Zhao
- 631
- 2
- 8
- 24
-
Does this answer your question? [Python - How to call bash commands with pipe?](https://stackoverflow.com/questions/7323859/python-how-to-call-bash-commands-with-pipe) – Maurice Meyer Jun 01 '20 at 18:48
-
@MauriceMeyer no it doesn't answer my question – Chang Zhao Jun 01 '20 at 18:51
1 Answers
0
According to the suggestions of the documentation, I would avoid using the low-level interface Popen
, which is tricky to use correctly in the long run, and to mindlessly use shell=True
, which might expose you to shell injection attacks.
I would therefore suggest a simpler approach if that's all you need: just capture the output and use it using the new capture_output
parameter of the new subprocess.run()
API.
This will get what you need:
completed_process = subprocess.run(['outercommand', subprocess.run(['which', 'somecommand'], capture_output=True).stdout], capture_output=True)
completed_process.check_returncode()
print(completed_process.stdout.decode('utf-8'))

Pentracchiano
- 188
- 7