Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wrapping cmd.exe with subprocess

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
like image 898
user246456 Avatar asked Mar 28 '26 22:03

user246456


1 Answers

Usually when trying to call command prompt with an actual command, it is simpler to just call it with the "/k" parameter rather than passing commands in via stdin. That is, just call "cmd.exe /k dir". For example,

from os import *
a = popen("cmd /k dir")
print (a.read())

The code below does the same thing, though lacks a string for you to manipulate, since it pipes to output directly:

from subprocess import *
Popen("cmd /k dir")
like image 93
Brian Avatar answered Mar 31 '26 03:03

Brian