Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - open new shell and run command

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

os.system(cmd)

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

Thanks, Dan

like image 516
Dan Avatar asked Oct 12 '12 11:10

Dan


2 Answers

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
like image 61
pmod Avatar answered Sep 28 '22 08:09

pmod


os.system() is deprecated in favour of :

import subprocess
print subprocess.check_output("command", shell=True)
like image 23
Gilles Quenot Avatar answered Sep 28 '22 08:09

Gilles Quenot