building strings with variable input
Erik Max Francis
max at alcyone.com
Mon Jan 12 11:15:20 CET 2004
Olaf Meyer wrote:
> Especially if you have a lot of variable input it makes it hard to
> the variables to the proper fields. From other scripting languanges
> used to something like:
> $cmd = "$executable -start $startTime -end $endTime -dir $directory"
> This makes it very easy to see how the string is actually built. You
> dont't have to worry where which variables go.
> Is there a similar way to do this in python?
cmd = "%(executable)s -start %(startTime)s -end %(endTime)s -dir
%(directory)s" % locals()
There are also more expansive solutions such as YAPTU or EmPy.
Note, however, that what you are trying to do (presuming you're passing
this to os.system or something similar) is potentially a serious
security risk. If the values of the strings you are constructing the
command line are not fully trustworthy, they can be easily manipulated
to make your program execute arbitrary shell commands.
__ Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ In the fight between you and the world, back the world.
-- Frank Zappa
More information about the Python-list