'How do I get all of the output from my .exe using subprocess and Popen?
I am trying to run an executable and capture its output using subprocess.Popen; however, I don't seem to be getting all of the output.
import subprocess as s
from subprocess import Popen
import os
ps = Popen(r'C:\Tools\Dvb_pid_3_0.exe', stdin = s.PIPE,stdout = s.PIPE)
print 'pOpen done..'
while:
line = ps.stdout.readline()
print line
It prints two line less than the original exe file when opened manually.
I tried an alternative approach with the same result:
f = open('myprogram_output.txt','w')
proc = Popen('C:\Tools\Dvb_pid_3_0.exe ', stdout =f)
line = proc.stdout.readline()
print line
f.close()
Can anyone please help me to get the full data of the exe?
As asked by Sebastian:
Original exe file last few lines o/p:
-Gdd : Generic count (1 - 1000)
-Cdd : Cut start at (0 - 99) -Edd : Cut end at (1 - 100)
Please select the stream file number below:
1 - .\pdsx100-bcm7230-squashfs-sdk0.0.0.38-0.2.6.0-prod.sao.ts
The o/p I get after running:
-P0xYYYY : Pid been interested
-S0xYYYY : Service ID been interested
-T0xYYYY : Transport ID been interested
-N0xYYYY : Network ID been interested
-R0xYYYY : A old Pid been replaced by this PID
-Gdd : Generic count (1 - 1000)
So we can see some lines missing. I have to write 1 and choose value after please select the fule number below appears.
I tried to use ps.stdin.write('1\n'). It didn't print the value in the exe file
New code:
#!/usr/bin/env python
from subprocess import Popen, PIPE
cmd = r'C:\Tools\Dvb_pid_3_0.exe'
p = Popen(cmd, stdin=PIPE, stdout=None, stderr=None, universal_newlines=True)
stdout_text, stderr_text = p.communicate(input="1\n\n")
print("stdout: %r\nstderr: %r" % (stdout_text, stderr_text))
if p.returncode != 0:
raise RuntimeError("%r failed, status code %d" % (cmd, p.returncode))
Thanks Sebastien. I am able to see the entire output but not able to feed in any input with the current code.
Solution 1:[1]
The indentation of your question threw me off a bit, since Python is particular about that. Have you tried something as so:
import subprocess as s
from subprocess import Popen
import os
ps = Popen(r'C:\Tools\Dvb_pid_3_0.exe', stdin = s.PIPE,stdout = s.PIPE)
print 'pOpen done..'
(stdout, stderr) = ps.communicate()
print stdout
I think that stdout will be one single string of whatever you return from your command, so this may not be what you desire, since readline() presumes you want to view output line by line.
Would suggest poking around http://docs.python.org/library/subprocess.html for some uses that match what you are up to.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | terryjbates |
