I try to wrap cmd.exe under windows with the following program but it doesn’t work , it seems to wait for something and doesn’t display anything. Any idea what is wrong here ?
import subprocess
process = subprocess.Popen('cmd.exe', shell=False, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=None)
process.stdin.write("dir\r\n")
output = process.stdout.readlines()
print output
This locks up because
process.stdout.readlines()reads all output by the process (until it terminates). Since cmd.exe is still running, it keeps waiting forever for it to close.To fix this, you can start a separate thread to read the process output. This is something you need to do anyway if you don’t call
communicate(), to avoid possible deadlock. This thread can callprocess.stdout.readline()repeatedly and handle the data or send it back to the main thread for handling.