converting from shell script to python

Greg Ewing (using news.cis.dfn.de) ckea25d02 at sneakemail.com
Tue Apr 1 19:40:38 EST 2003


Mathias Waack wrote:
> import os
> 
> ROOTDIR='/usr/lib'
> CLASSPATH="%s/a/a.jar:%s/b/b.jar:%s/c/c.jar" % \
>           (ROOTDIR,ROOTDIR,ROOTDIR)
> os.system('java -classpath %s foo' % (CLASSPATH))

Alternatively, you can substitute things from a
dictionary instead of a tuple:

   vars = {'ROOTDIR': '/usr/lib'}
   CLASSPATH = \
     "%{ROOTDIR}s/a/a.jar:%{ROOTDIR}s/b/b.jar:%{ROOTDIR}s/c/c.jar" % vars

That technique can be more readable if you have
more variables that you want to substitute into
the string, since you can name them where they're
used instead of relying on positional matching.

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg





More information about the Python-list mailing list