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!
It is better to use the subprocess
module documentation here, 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,
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
>>> subprocess.check_output(["echo", "Hello World!"])
'Hello World!\n'