How to eval a file

Donnal Walter donnal at donnal.net
Sun Feb 23 16:08:50 EST 2003


Alex Martelli wrote:
> Björn Lindberg wrote:
>>
>>I found the shlex module which made it really easy to parse my
>>configuration file, so I'm doing that now.
> 
> 
> OK, if you can stay away from eval/exec/execfile altogether, you'll no
> doubt be even gladder;-).

I've been thinking about making a configuration file with a syntax 
something like this:

{
   'log': './error.txt'
   'splash': './welcome.png'
   'icon': './prism.ico'
   'login': 'C:/prism/user.dbm'
   'cabinet': 'C:/prism/patient.dbm'
}

Then I planned on reading the file something like this:

         f = open(filename)
         txt = f.read().replace('\r\n','\n')
         f.close()
         self.__dict__.update(eval(txt))

Do I understand your previous reply to Björn to mean that I would still 
be better off using ConfigParser?

Donnal Walter
Arkansas Children's Hospital








More information about the Python-list mailing list