0

As an add-on to my old question: I have a question. When I run cmd.exe and execute the target program, the output is printed to cmd nicely and it exits with code 0 in the end,

Since the program continuously prints to my cmd.exe stdout, how come I can't mimic that behaviour in Python?

The following code is how I parse lines from my target executable.

res = subprocess.Popen(command, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1)
with res.stdout:
     for line in iter(res.stdout.readline, b''):
         print line
res.wait()

The python parsing doesnt even read the same things as cmd.exe does! It doesnt print the last 5-10 lines (the ones telling me the process is complete).

Do I have to subprocess popen cmd.exe then call the target program? Are there any other alternatives?

Community
  • 1
  • 1
enrm
  • 645
  • 1
  • 8
  • 22
  • You seem to be confusing cmd.exe with the console window (hosted by conhost.exe). cmd.exe simply waits on the child process handle when you run another console program such as chcp.com, mode.com, powershell.exe, or python.exe, so mimicking how cmd.exe runs a console program is as simple as `exitcode = subprocess.call(command)`. That's assuming you don't have to set up a pipeline, which cmd.exe does in a simpler way than how subprocess works, because it's a single threaded application. – Eryk Sun Jul 12 '16 at 13:30
  • @eryksun thanks, that did it. If you post it as an aswer i will accept it – enrm Jul 13 '16 at 13:12

0 Answers0