Ionut Hulub Ionut Hulub - 1 year ago 153
Python Question

subprocess.Popen in different console

I hope this is not a duplicate.

I'm trying to use

to open a script in a separate console. I've tried setting the
parameter but that didn't do the trick.

I use a 32 bit Python 2.7 on a 64 bit Windows 7.

Answer Source
from subprocess import *

c = 'dir' #Windows

handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE, shell=True)

If you don't use shell=True you'll have to supply Popen() with a list instead of a command string, example:

c = ['ls', '-l'] #Linux

and then open it without shell.

handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE)

This is the most manual and flexible way you can call a subprocess from Python. If you just want the output, go for:

from subproccess import check_output
print check_output('dir')

To open a new console GUI window and execute X:

import os
os.system("start cmd /K dir") #/K remains the window, /C executes and dies (popup)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download