subprocess.Popen在不同的控制台中 [英] subprocess.Popen in different console
问题描述
我希望这不是重复的内容.
I hope this is not a duplicate.
我正在尝试使用subprocess.Popen()
在单独的控制台中打开脚本.我试过设置shell=True
参数,但没有成功.
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.
我在64位Windows 7上使用32位Python 2.7.
I use a 32 bit Python 2.7 on a 64 bit Windows 7.
推荐答案
from subprocess import *
c = 'dir' #Windows
handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE, shell=True)
print handle.stdout.read()
handle.flush()
如果不使用shell=True
,则必须为Popen()
提供列表而不是命令字符串,例如:
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()
这是您可以从Python调用子流程的最手动,最灵活的方法. 如果只需要输出,请执行以下操作:
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')
要打开新的控制台GUI窗口并执行X:
import os
os.system("start cmd /K dir") #/K remains the window, /C executes and dies (popup)
这篇关于subprocess.Popen在不同的控制台中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!