How to get the output from os.system() into a variable?

Laszlo Zsolt Nagy gandalf at designaproduct.biz
Wed Oct 5 09:53:42 EDT 2005


alexLIGO wrote:

>Hi,
>
>I would like to execute some command in python on the bash, e.g.
>
>os.system('globus-job-run mike4.cct.lsu.edu/jobmanager-pbs -l
>/bin/date')
>
>and want the result of the output in a vector, so something like:
>
>result=python_command(' command_on_the_bash ')
>
>Is that possible? And how to do that?
>  
>
You can use one of these:

the subprocess module: http://docs.python.org/lib/module-subprocess.html
the popen2 module: http://docs.python.org/lib/module-popen2.html
popen and spawn variants from the os module:  
http://docs.python.org/lib/os-process.html
pexpect (it is not in the standard library):  
http://pexpect.sourceforge.net/

I believe that if you do not need the extended functionality of 
"pexpect", then you should start with the "subprocess" module.
It is the newest.

   Les






More information about the Python-list mailing list