subprocess.Popen 在不同的控制台 [英] subprocess.Popen in different console

查看:35
本文介绍了subprocess.Popen 在不同的控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这不是重复的.

我正在尝试使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆