os.system for submitting command
Question: I am using os.system to submit a command to the system.

I.e.,

    
    
    import os
    os.system(my_cmd)
    

But I was wondering how could I obtain the output, i.e., let us say i am in
the bash and I type in my cmd, I'd get an output of this form:

    
    
    Job <57960787> is submitted to queue <queueq>.
    

How can I, in python, using the os.system(cmd), also obtain the text output,
and parse it to obtain the job id, 57960787.

Thanks!


Answer: It is better to use the `subprocess` module documentation
[here](http://docs.python.org/2/library/subprocess.html), example below:

    
    
    import subprocess,re
    p = subprocess.Popen('commands',stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    results, errors = p.communicate()
    print results
    re.search('<(\d+)>', results).group(1) #Cheers, Jon Clements
    

Or you can even use `os.popen` documentation
[here](http://docs.python.org/2/library/os.html#os.popen),

    
    
    p_os = os.popen("commands","r")
    line = p_os.readline()
    print line
    re.search('<(\d+)>', line).group(1) #Cheers, Jon Clements
    

Or as John Clements kindly suggested, you can use `subprocess.check_output`,
Documentation
[here](http://docs.python.org/2/library/subprocess.html#subprocess.check_output)

    
    
    >>> subprocess.check_output(["echo", "Hello World!"])
    'Hello World!\n' 
    

