1

I want to call python argument inside string. Is that possible? Suppose I have declared variable mypath as

mypath = os.path.abspath('1.jpg')

and I need to call it inside another variable that is string like following

cmd0 = 'gimp-console-2.8 -b -idf --batch-interpreter python-fu-eval -b "import sys,os;sys.path=[\'.\']+sys.path;import mycode;'
cmd1 = 'mycode.doit('THIS VARIABLE MYPATH SHOULD GO HERE')"'
cmd2 = '-b "pdb.gimp_quit(0)"'
cmdfinal = cmd0 + cmd1 + cmd2
os.system(cmdfinal)
4
  • Not sure what you mean... you can use the value in a number of ways, including "using file %s" % mypath. Commented Sep 17, 2014 at 1:14
  • 1
    What do you mean by "call it"? Or by "argument"? While it's possible to pass a function or other callable object as an argument, your example isn't doing anything remotely similar. Show us the code that you want to write (with a # I'm stuck here line) and explain what it shoul ddo. Commented Sep 17, 2014 at 1:14
  • Explained a little better now. Commented Sep 17, 2014 at 1:15
  • 1
    OK, better. Are you just trying to insert the string into cmd1? If so, the simplest way is cmd1 = 'mycode.doit({0})'.format(mypath). Commented Sep 17, 2014 at 1:15

2 Answers 2

1

It looks like what you want to do is insert a variable into the middle of a string.

This is done with string formatting. Python has a couple different ways of doing it, but the simplest is the str.format method, as explained in the tutorial section Fancier Output Formatting:

cmd1 = 'mycode.doit({0})'.format(mypath)

if mypath is, say, the string /Users/me/Documents/1.jpg, then cmd1 will be the string mycode.doit(/Users/me/Documents/1.jpg).

But that's not quite right, because you're trying to format a command that can be run by another Python interpreter. So, you want not just the contents of the string, but a Python literal for the string. How do you do that?

The repr of a string is the string in quotes, with proper escaping to make sure it can be used directly in Python code. (This isn't true for all objects; for example, some will return something like <Spam object at 0x12345678>, which isn't particularly useful; it's generally only true for objects where typing the output into Python code would make sense, and get you an exactly equivalent object.)

So, how do you get that? As explained in Format String Syntax, you use the r conversion specifier:

cmd1 = 'mycode.doit({0!r})'.format(mypath)

Now, cmd will be the string mycode.doit('/Users/me/Documents/1.jpg')

And that's exactly what you want.

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

Comments

1

You do use a tuple

mypath = os.path.abspath('1.jpg')
cmd = 'gimp-console-2.8 -b -idf --batch-interpreter python-fu-eval -b "import sys,os;sys.path=[\'.\']+sys.path;import mycode;mycode.doit(\'%s\')" -b "pdb.gimp_quit(0)"' % (mypath)
os.system(cmd)

Or using a format string

mypath = os.path.abspath('1.jpg')
cmd = 'gimp-console-2.8 -b -idf --batch-interpreter python-fu-eval -b "import sys,os;sys.path=[\'.\']+sys.path;import mycode;mycode.doit(\'{0}\') -b "pdb.gimp_quit(0)"'.format(mypath)
os.system(cmd)

Comments

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.