0

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?

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

If i do: temp = os.system("amixer get Master |grep Mono:") & print temp i get output result. Or i'm wrong?
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)
1

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()

Comments

0

How to run a process and get the output:

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

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.