tags:

views:

71

answers:

3

Hi guys, i have to parse need string. Here is command I execute in Linux console:

amixer get Master |grep Mono:

And get, for example,

Mono: Playback 61 [95%] [-3.00dB] [on]

Then i test it from python-console:

import re,os
print re.search( ur"(?<=\[)[0-9]{1,3}", u"  Mono: Playback 61 [95%] [-3.00dB] [on]" ).group()[0]

And get result: 95. It's that, what i need. But if I'll change my script to this:

print re.search( ur"(?<=\[)[0-9]{1,3}", str(os.system("amixer get Master |grep Mono:")) ).group()[0]

It'll returns None-object. Why?

+7  A: 

os.system() returns the exit code from the application, not the text output of the application.

You should read up on the subprocess Python module; it will do what you need.

retracile
Ockonal
The output from the command is going to stdout directly, and not to the 'temp' variable. Try running that test with 'print "the value of temp is %s characters long, and is: %s" % (len(temp), temp)
retracile
Thanks, now i understand
Ockonal
A: 

How to run a process and get the output:

http://docs.python.org/library/popen2.html

Stefan Kendall
Deprecated since version 2.6: This module is obsolete. Use the `subprocess` module.
SilentGhost
Which is linked to in that documentation, but I found popen to be more clear. Thanks for the downvote, though.
Stefan Kendall
+1  A: 

Instead of using os.system(), use the subprocess module:

from subprocess import Popen, PIPE
p = Popen("amixer get Master | grep Mono:", shell = True, stdout = PIPE)
stdout = p.stdout.read()
print re.search( ur"(?<=\[)[0-9]{1,3}", stdout).group()
John Millikin
Thank you for example too.
Ockonal