I hope this is not a duplicate.
I'm trying to use subprocess.Popen()
to open a script in a separate console. I've tried setting the shell=True
parameter but that didn't do the trick.
I use a 32 bit Python 2.7 on a 64 bit Windows 7.
To open in a different console, do (tested on Win7 / Python 3):
from subprocess import Popen, CREATE_NEW_CONSOLE
Popen('cmd', creationflags=CREATE_NEW_CONSOLE)
input('Enter to exit from Python script...')
How can I spawn new shells to run python scripts from a base python script?
from subprocess import *
c = 'dir' #Windows
handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE, shell=True)
print handle.stdout.read()
handle.flush()
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)
print handle.stdout.read()
handle.flush()
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')
import os
os.system("start cmd /K dir") #/K remains the window, /C executes and dies (popup)
On Linux shell=True will do the trick:
command = 'python someFile.py'
subprocess.Popen('xterm -hold -e "%s"' % command)
Doesn't work with gnome-terminal as described here:
dir
in windows andls
in linux for listing folder contents.popen
in the new console. In my main program, I don't need the output of the program I run withpopen
. I just need to see it in a different console. Could you please provide an example of how this should be implemented?.Popen()
does, and if you check your process monitor you'll notice that a cmd.exe is launched from you calling.Popen()
, it's just not visible because it's a subprocess to your application and it's "muted". You don't typically open new GUI windows just to execute tasks, so may i ask why you want this? because it sounds backwards to me :/X
with my first example, grab the exit code and print only valuable information if needed and ignore the rest of the output. It's faster and more elegant. Anyways,os.system()
gives you the returncode of your command and the program will wait for the command to finish since you're not utelizing threads, see docs.python.org/2/library/threading.html for help with threads.