1

I'm trying to write a python program on my Raspberry Pi to mute/change the volume of the speaker output as soon as it runs. If I type:

amixer sset 'Master' 50%

on the terminal, the volume is increased by 50%.

However if I write a python file called filename.py with the following lines:

import os
os.system("amixer sset 'Master' 50%")

and run it in terminal with

sudo python filename.py

it comes up with an error

amixer: Unable to find simple control 'Master',0

How can I just run exactly the same command in terminal as I do in python?

  • os.system(...) takes a string as an argument. You probably meant to write os.system("amixer sset 'Master' 50%") – MadMike Feb 21 '17 at 10:11
  • Sorry thats what I meant. I edited the post, thanks for pointing that out. – mnt Feb 21 '17 at 10:16
  • Why do you call sudo python filename.py instead of just python filename.py? You don't seem to need sudo with amixer? – MadMike Feb 21 '17 at 10:19
  • Wow! such a simple solution and its worked! Ive been trying all sorts of complicated solutions. Thanks MadMike, Im new the the forum, how can I rate this comment up? – mnt Feb 21 '17 at 10:24
  • 1
    I'm going to formulate it as a answer. I may then click the check-mark on the left to accept it. – MadMike Feb 21 '17 at 10:26
0

You need to change your filename.py from

import os
os.system(amixer sset 'Master' 50%)

to

import os
os.system("amixer sset 'Master' 50%")

as os.system(...) only accepts a string as a argument.

Additional points:

  • Don't call sudo python filename.py if your program don't need root rights. In your case python filename.py should suffice.
  • It's good form to add #!/usr/bin/python to the beginning of your script. This indicates it's a python script. So your file will looks like this:

    #!/usr/bin/python
    
    import os
    os.system("amixer sset 'Master' 50%")
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.