|
From: Michael F. <fuz...@vo...> - 2008-08-26 17:25:52
|
Antonis Papadopoulos wrote:
> Hello
>
> I have an .ini (settings file) which contains following:
>
> Code:
>
> [section1]
> item1=value
> item2=value
>
> [section2]
> item3=value
>
> I put the "test.ini" and the "configobj.py" files in "E:\".
>
> I then run the following script (iniread.py):
>
> Code:
>
> import sys
> sys.path.append("E:\\")
> import configobj
> from configobj import ConfigObj
>
> config = ConfigObj("test.ini")
>
> section = config['section2']
> value = section['item3']
>
> print 'item3 : ' + value
>
> But I get the following error:
>
> Quote:
> Traceback (most recent call last):
> File "C:\private\2000b1a5\default.py", line 81, in menu_action
> f()
> File "C:\private\2000b1a5\default.py", line 65, in query_and_exec
> execfile(script_list[index][1]. encode('utf-8'), script_namespace. namespace)
> File "C:\python\iniread.py", line 8, in ?
> section = config['section2']
> File "E:\configobj.py", line 580, in __getitem__
> val = dict.__getitem__(self, key)
> KeyError: section2
>
> What am I doing wrong? Why can't I see the value of item3?
> I also want to know how to read a file that is not in the same folder
> as configobj.py I tried with E:\\foldername\test.ini but I got a parse
> error in configobj.py...
>
In the first case you are passing ConfigObj a relative path - so because
the config file 'test.ini' doesn't exist in the current directory
ConfigObj creates a new one.
When you provide the full path the parse error *should* tell you what is
wrong. I think you need spaces around your '=' sign - so that ConfigObj
can tell where your keys end and your values begin.
item1 = value
Try that and either change the current directory to the location
containing the config file or pass in the full path.
Michael Foord
> Thank you
>
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
> Build the coolest Linux based applications with Moblin SDK & win great prizes
> Grand prize is a trip for two to an Open Source event anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> Configobj-develop mailing list
> Con...@li...
> https://lists.sourceforge.net/lists/listinfo/configobj-develop
>
--
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/
http://www.trypython.org/
http://www.ironpython.info/
http://www.theotherdelia.co.uk/
http://www.resolverhacks.net/
|