[Tutor] Looping + Variables
James
jtp at nc.rr.com
Sun Oct 21 23:54:50 CEST 2007
Hi. :)
I'm trying to write a loop to simplify my life (and code :)). The
loop is going to iterate over a list of values that I have to change
in a file. I think my problem is better described with some code. :)
# variables
interface = "eth0"
address = "192.168.1.5"
mask = "255.255.255.0"
gateway = "192.168.1.1"
def replaceText( old , new , file ):
for line in fileinput.FileInput( file , inplace = 1 ):
line = line.replace( old , new )
sys.stdout.write( line )
editValues = ( 'interface' , 'address' , 'mask' , 'gateway' )
for value in editValues:
foo( '$' + value + '$' , value , '/etc/conf.d/net' )
I need to edit the values listed in "editValues" in a specific file.
The file looks something like this before the code runs:
config_$interface$=( "$address$ netmask $mask$" )
routes_$interface$=( "default via $gateway$" )
As you can see, the values that I want to edit are originally in the
file as $variable$. The goal of my loop is to iterate over the
editValues tuple and edit the $variable$ and put the ACTUAL variable
value. :)
After running my code, the file looks like this:
config_interface=( "address netmask mask" )
routes_interface=( "default via gateway" )
If I put some debug statements in the function replaceText(), it
shows that what is passed in is actually the value "address" and
"interface", instead of "192.168.1.5" or "eth0".
Obviously that's not what I want. :)
I imagine I'm missing something obvious. Doh! Thoughts? Ideas?
Thanks!
.james
More information about the Tutor
mailing list