Using ConfigParse

Peter Otten __peter__ at web.de
Tue Jun 8 09:02:21 EDT 2004


Zunbeltz Izaola wrote:

> Peter Otten <__peter__ at web.de> writes:
> 
>> Zunbeltz Izaola wrote:
>> 
>> >     def save(self):
>> >         print "Enter save"
>> >         self.write(open('defaultdifrac.cfg','w'))
>> >         self.read(open('defaultdifrac.cfg'))
>> >         print "OUt save"
>>  
>> I would explicitly close the file before trying to read it.
>> 
>> >>> def save(fn="tmp.txt", s="so what", close=True):
>> ...     f = file(fn, "w")
>> ...     f.write(s)
>> ...     if close: f.close()
>> ...     return file(fn).read()
>> ...
>> >>> save()
>> 'so what'
>> >>> save(s="another")
>> 'another'
>> >>> save(s="yet another", close=False)
>> ''
> 
> Thanks for the sugestion but it doen't work, i don't know wy
> CofigParser doesn't write the file

I looked it up in the source: the read() method expects a filename or a
sequence of filenames. Incidentally that means, that every line in your
config-file is interpreted as a filename. It doesn't matter that none of
these files exist, as all IOErrors are silenced by the method.

self.readfp(open('defaultdifrac.cfg')) 

or

self.read("defaultdifrac.cfg")

in your save() method instead of self.read(open('defaultdifrac.cfg')) should
fix the problem. That would of course mean that ConfigParser *does* write
the file, only doesn't read it back. Can you confirm that?

Peter




More information about the Python-list mailing list