

Sorry for the long-winded response, but I am frustrated on this board with the many terse ‘answers’ which at best don’t work (seems because they are not tested – like Eduard F’s response above mine which is missing the switch) or worse, are so terse that they don’t help much at all (e.g., ‘try subprocess instead of os.system’ … yeah, OK, now what ?). Changing the switch to /C gives us all the output expected AND returns to the python prompt since the subprocess terminates in accordance with /C. Adding the /K switch prints out the directory listing and changes the prompt from python to DOS since /K does not terminate the subprocess. Without the switch, again the “dir” part is ignored, AND the prompt changes from the python prompt to the DOS prompt (since a cmd.exe subprocess is running in python again type ‘exit’ and you will revert to the python prompt). The only way I managed to get first 3 lines was by simply taking first 3 items from the list.

However, I am interested only in first 3 lines of the stdout.
#Python subprocess get output windows code#
Currently my code saves all the lines of stdout to a list ('lines' in the code below). If I try the same thing (subprocess.call instead of subprocess.check_output) from a DOS Python command window all output is within the same window, there are no popup windows. I am using subprocess module to execute a process and read stdout returned by it. When I instead add the /K switch, the DOS window pops open and remain, AND I get all the output I expect including the directory listing. When I add the /C switch, the DOS window opens and closes very fast before I can see anything (as expected, because /C terminates when done). …a new DOS window pops open and remains there displaying only the results of “cmd.exe” not of “dir”. R = subprocess.call("cmd.exe dir",shell=False) Went a step further…I thought this was cool…When I instead do this in Expts.py: import subprocess

But with the /K switch it works perfectly and returns you to the python prompt. Now, if I try any of those same things in DOS Python command window, without the switch, or with the /K switch, it appears to make the window hang because it is running a subprocess cmd.exe and it awaiting further input – type ‘exit’ then hit to release. …then in the Python Shell, I get all that I expect including the directory listing. R = subprocess.check_output('cmd.exe /K dir',shell=False) HOWEVER, when I add a switch such as /K or /C … import subprocess …in the Python Shell, I ONLY get the output corresponding to “cmd.exe” the “dir” part is ignored. > result n( 'python3', '-version', captureoutputTrue, encoding'UTF-8') > result. We can achieve this with the captureoutputTrue option: > import subprocess. R = subprocess.check_output('cmd.exe dir',shell=False) If you run an external command, you’ll likely want to capture the output of that command. Using IDLE with Python 2.7.5, When I run this code from file Expts.py: import subprocess
#Python subprocess get output windows windows#
I would like to expand on the Windows solution.
