import os, subprocess
p = subprocess.Popen("cmd.exe", stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
print>>p.stdin, "echo hi"
p.stdout.readline()
p.stdout.readline()
p.stdout.readline()
p.stdout.readline()
p.stdout.readline()
p.stdout.readline()
print>>p.stdin, "python"
p.stdout.readline()
Now, if I do p.stdout.readline(), why don’t I see a python shell?
On the other hand, If instead of python, I had started another cmd from the subprocess, I can see a new cmd shell spawning.
import os, subprocess
p = subprocess.Popen("cmd.exe", stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
print>>p.stdin, "echo hi"
p.stdout.readline()
p.stdout.readline()
p.stdout.readline()
p.stdout.readline()
p.stdout.readline()
p.stdout.readline()
print>>p.stdin, "cmd"
p.stdout.readline()
p.stdout.readline()
p.stdout.readline()
What’s the difference?
Python behaves differently when its standard output is not a terminal device: no prompt, no banner, it just reads a complete script and executes it.
I don’t know exactly how it was ported to Windows, but “console handle” is the closes thing to “terminal device” they have.