Friday, 16 August 2013

Python, Capture the result from a terminal (external process)

Python, Capture the result from a terminal (external process)

From within Python, I need to get the result of a command from the terminal:
mask = "audio"
a = os.system("ls -l | grep %s | awk '{ print $9 }'" % mask)
print a # a = 0, that's the exit code
#=>
file1_audio
file2_audio
0
This command just prints 0, the exit code, result to the console, while I
want to capture the result of the command to a variable.

No comments:

Post a Comment