1

I would like to execute multiple commands within the command window I opened thru the subprocess.pOpen command ? how to do it ?

Something like

p = subprocess.Popen("start cmd /k ", stdin=subprocess.PIPE, stdout=subprocess.PIPE,shell=True )

now I want to call multiple commands in the same window I opened. I don't want to pass the argument as a list since it will only be treated as parameters of the first element in the list.

Dinesh Pundkar
  • 4,160
  • 1
  • 23
  • 37
Prasad Nair
  • 485
  • 2
  • 6
  • 12
  • 1
    Why don't you create a batch file and call that batch file? Or do you need any interaction between that command prompt and this python program – be_good_do_good Aug 23 '16 at 14:35
  • 1
    Please check this link - http://stackoverflow.com/questions/5486725/how-to-execute-a-command-prompt-command-from-python – Dinesh Pundkar Aug 23 '16 at 14:35
  • If you need interaction or send the commands one by one, whatever Dinesh suggested is good – be_good_do_good Aug 23 '16 at 14:38
  • I want to issue command based on the messages I am getting into the window. So running a batch file ahead of time is not a solution for me. – Prasad Nair Aug 23 '16 at 15:47

1 Answers1

-1

Following is the snippet which I keep it handy always to run windows commands.

import subprocess
result = []
win_cmd = 'ipconfig'
#shell=True means do not show the command shell
#shell=False means show the command shell
process = subprocess.Popen(win_cmd,
            shell=False,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE )
for line in process.stdout:
    print (line)
result.append(line)
errcode = process.returncode
for line in result:
    print (line)