Python-打开新的Shell并运行命令 [英] Python - open new shell and run command

查看:121
本文介绍了Python-打开新的Shell并运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我正在Python中使用以下方法运行bash命令:

At the moment I am running a bash command from within Python using the following method:

os.system(cmd)

但是我需要在新的shell /终端中运行命令。有人知道怎么做吗?

However I need to run the command in a new shell/terminal. Does anyone know how to do this?

谢谢,

推荐答案

我正在使用以下方法(这也会将stderr重定向到stdout):

I am using the following method (this will also redirect stderr to stdout):

import subprocess    
cmd_line = "echo Hello!"
p = subprocess.Popen(cmd_line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out = p.communicate()[0]
print out

这篇关于Python-打开新的Shell并运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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