executing a variable?

Greg Krohn volucris at hotmail.com
Wed Oct 10 05:33:37 CEST 2001

The ConfigParser module is what you want. If you are familiar with Windows
.ini files, it works a lot like those.

import ConfigParser

cf1 = ConfigParser.ConfigParser()
cf1.set('MAIN', 'spam', 'foo')
cf1.set('MAIN', 'eggs', 'bar')
cf1.write(open('settings.text', 'w'))

cf2 = ConfigParser.ConfigParser()
print 'spam is ' + cf2.get('MAIN', 'spam')
print 'eggs is ' + cf2.get('MAIN', 'eggs')



Volucris (a) hotmail.com
"Eu não falo uma única palavra do português."

"Jeremy Whetzel" <lists at toadmail.com> wrote in message
news:87ofnf9owk.fsf at toadmail.com...
> Hi,
> I'm playing around with a script that creates a "settings" file to be
> loaded the next time the program is run.  Currently what I have is a
> settings file that is just a number of variables defined:
> spam = "foo"
> eggs = "bar"
> ... and so on...
> Then I do something like:
> settings = open("settings.text","r")
> line1 = settings.readline()
> line2 = settings.readline()
> ... and so on...
> now I have these variables that contain the whole line of 'spam =
> "foo"'.  Is there anyway I can "execute" these variables so that they
> actually perform the instructions inside them?  If I try to " print
> spam", I get NameError spam.
> OR, if there is a better way of doing this, please point me in the
> correct direction.  I'm extremely new to python, and only slightly less
> so with shell scripting (to give you an idea of my programming prowess).
> Thanks for your help,
> Jeremy

More information about the Python-list mailing list