Retaining an object

S

sysfault

Hello, I have a function which takes a program name, and I'm using
os.popen() to open that program via the syntax: os.popen('pidof var_name',
'r'), but as you know var_name is not expanded within single quotes, I
tried using double quotes, and such, but no luck. I was looking for a way
to have var_name expanded without getting a syntax error by ommiting the
surrounding quotes. I need to use a variable, it's the argument to a
function.
 
G

gry

sysfault said:
Hello, I have a function which takes a program name, and I'm using
os.popen() to open that program via the syntax: os.popen('pidof var_name',
'r'), but as you know var_name is not expanded within single quotes, I
tried using double quotes, and such, but no luck. I was looking for a way
to have var_name expanded without getting a syntax error by ommiting the
surrounding quotes. I need to use a variable, it's the argument to a
function.

Use the string format operator "%":

var_name='magick'
os.popen('pidof %s' % var_name, 'r')

this results in running:
pidof magick

You should really read through the Python tutorial to get basic stuff
like this:

http://docs.python.org/tut/tut.html
 
L

Leif K-Brooks

sysfault said:
I'm using os.popen() to open that program via the syntax:
os.popen('pidof var_name', 'r'), but as you know var_name is not
expanded within single quotes,

os.popen('pidof %s' % var_name, 'r')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,311
Members
47,986
Latest member
ColbyG935

Latest Threads

Top