MichaelR
MichaelR

Reputation: 196

Redirect stdout of one Popen to another Popen stdin

I am trying to send the output of one process as an input to another process using the following code block:

p1 = Popen(command, stdout=PIPE)
p2 = Popen(dgxmcmd, stdin=p1.stdout, stdout= PIPE, stderr = PIPE)
output = p2.communicate()[0]

But the p2 is getting an exception as no such file or directory. It seems the p1 stdout terminates.

Traceback (most recent call last):
File "/usr/bin/toolbox", line 160, in <module>
d.brokerProfile()
File "/usr/lib/python2.7/site-packages/toollib/deploy.py", line 340, in 
brokerProfile
self.dgxm(cmd1)
File "/usr/lib/python2.7/site-packages/toollib/deploy.py", line 327, in dgxm
p2 = Popen([dgxmcmd], stdin = p1.stdout, stdout = PIPE, stderr = PIPE)
File "/usr/lib64/python2.7/subprocess.py", line 390, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1024, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

Anyway to figure out what is going on here? Thanks in advance.

Additional Information: dgxm is a third party tool which accepts commands. Commands are dynamically generated from p1 and then sent to p2 for execution using the third party tool.

Upvotes: 2

Views: 525

Answers (1)

Inder
Inder

Reputation: 3826

instead just do this

import os
p1 = os.popen(command)
p1=p1.read()
p2 = Popen(dgxmcmd, stdin=p1, stdout= PIPE, stderr = PIPE)
output = p2.communicate()[0]

that should work if there is no further information

Upvotes: 2

Related Questions