Tuesday, 17 September 2013

Python - % Symbol and Invalid Syntax

Python - % Symbol and Invalid Syntax

I'm creating a Python script for my Raspberry Pi Radio. The Pi used
mpd/mpc to play music, and I can type in the following command in Terminal
to see information about the currently playing station:
mpc current -f "[%position%]"
This will show me:
11
As the 11th radio station is playing.
My problem is, when I put this in Python to extract this number to use as
part of my code, it gives me this: (I get a ^ under the first % symbol)
f=os.open("mpc current -f "[%position%]"")
SyntaxError: invalid syntax
This seems odd as I have already used similar commands in a Python script
which have worked, however they have not had the % symbol in them. Such
as:
f=os.popen("mpc current")
I am still learning Python so would appreciate it if someone could correct
me here, as the command seems legit to me, especially with the "" either
side. I even tried using ' instead of ", but if I do, the command doesn't
work.
Many thanks

No comments:

Post a Comment