473,320 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

config parser - modified

I was wondering if anyone can explain to me why I keep getting this error:

Traceback (most recent call last):
File "C:/Documents and Settings/William/Desktop/test.py", line 4, in <module>
value = INI.getinifile ( 'P:/INI/test.ini', 'Testsection', 'Testoption', 'Default Value' )
File "C:\Python25\lib\INIfile.py", line 8, in getinifile
if len(Value) == 0:
AttributeError: 'str' object has no attribute 'len'
Here is the code of the two files:

INIfile.py:
Expand|Select|Wrap|Line Numbers
  1. from ConfigParser import ConfigParser
  2. class INI:
  3.     def getinifile ( self, FILE, SECTION, ITEM, DEFAULT ):
  4.         p = ConfigParser()
  5.         p.read(FILE)
  6.         Value = p.get ( SECTION, ITEM ).lstrip().rstrip()
  7.         if len(Value) == 0:
  8.             Value = DEFAULT
  9.         return Value
test.py
Expand|Select|Wrap|Line Numbers
  1. import INIfile
  2.  
  3. INI = INIfile.INI()
  4. value = INI.getinifile ( 'P:/INI/test.ini', 'Testsection', 'Testoption', 'Default Value' )
  5. print value
I just don't see what I'm doing wrong.
Mar 30 '07 #1
3 1817
bvdet
2,851 Expert Mod 2GB
I was wondering if anyone can explain to me why I keep getting this error:



Here is the code of the two files:

INIfile.py:
Expand|Select|Wrap|Line Numbers
  1. from ConfigParser import ConfigParser
  2. class INI:
  3.     def getinifile ( self, FILE, SECTION, ITEM, DEFAULT ):
  4.         p = ConfigParser()
  5.         p.read(FILE)
  6.         Value = p.get ( SECTION, ITEM ).lstrip().rstrip()
  7.         if len(Value) == 0:
  8.             Value = DEFAULT
  9.         return Value
test.py
Expand|Select|Wrap|Line Numbers
  1. import INIfile
  2.  
  3. INI = INIfile.INI()
  4. value = INI.getinifile ( 'P:/INI/test.ini', 'Testsection', 'Testoption', 'Default Value' )
  5. print value
I just don't see what I'm doing wrong.
William,

It looks like it should work. After 'p.read(FILE)', add these to see what data is available:
Expand|Select|Wrap|Line Numbers
  1. print p.sections()
  2. print p.items(SECTION)
Add 'print Value' after Value is assigned to see what the object is.
Comment: str.strip() does the same thing as str.lstrip().rstrip().
Mar 30 '07 #2
Okay, It actually worked great after restarting IDLE, I guess a bad version of my script got caught in IDLE. Thanks.

Also, thanks for that last part. :)

I also had to change it a bit for the default value to work, but I got it working nicely.
Mar 31 '07 #3
Hmm...Now I am having a similar problem with the other function I need to finish the script, But restarting doesn't seem to help.

Traceback (most recent call last):
File "C:\Documents and Settings\William\Desktop\test.py", line 3, in <module>
INI.writeinifile ( 'P:/INI/test.ini', 'Testsection', 'Testoption2', 'koo koo ka choo' )
File "C:\Python25\lib\INIfile.py", line 28, in writeinifile
p.write(FILE)
File "C:\Python25\lib\ConfigParser.py", line 369, in write
fp.write("[%s]\n" % section)
AttributeError: 'str' object has no attribute 'write'
>>>
test.py
Expand|Select|Wrap|Line Numbers
  1. import INIfile
  2. INI = INIfile.INI()
  3. INI.writeinifile ( 'P:/INI/test.ini', 'Testsection', 'Testoption2', 'koo koo ka choo' )
INIfile.py
Expand|Select|Wrap|Line Numbers
  1. from ConfigParser import ConfigParser
  2.  
  3. class INI:
  4.     def getinifile ( self, FILE, SECTION, ITEM, DEFAULT ):
  5.         p = ConfigParser()
  6.         p.read(FILE)
  7.         if p.has_section ( SECTION ):
  8.             print 'Has Section'
  9.             if p.has_option ( SECTION, ITEM ):
  10.                 print 'Has Option'
  11.                 Value = p.get ( SECTION, ITEM ).strip()
  12.             else:
  13.                 Value = DEFAULT
  14.         else:
  15.             Value = DEFAULT
  16.         return Value
  17.     def writeinifile ( self, FILE, SECTION, ITEM, VALUE ):
  18.         p = ConfigParser()
  19.         p.read(FILE)
  20.         if p.has_section(SECTION):
  21.             pass
  22.         else:
  23.             p.add_section(SECTION)
  24.         if p.has_option(SECTION, ITEM):
  25.             pass
  26.         else:
  27.             p.set(SECTION, ITEM, VALUE)
  28.         p.write(FILE)
Mar 31 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: qqcq6s59 | last post by:
Hi all I am a newbie and I just saw a ongoing thread on Fileprocessing which talks abt config parser. I have writen many pyhton program to parse many kind of text files by using string module and...
25
by: n3crius | last post by:
hi, i just got a web host with asp.net , seemed really cool. aspx with the c# or vb IN the actual main page run fine, but when i use codebehind and make another source file ( a .cs) to go with...
4
by: Matt | last post by:
The ASP.NET application connects to MS-SQL database, and I already disabled Anonymous access in IIS, and add ASPNET user in it. When I run the application, it throws the following error on windows...
20
by: tomerfiliba | last post by:
hey i've been seeing lots of config-file-readers for python. be it ConfigObj (http://www.voidspace.org.uk/python/configobj.html) or the like. seems like a trend to me. i came to this conclusion...
10
by: Ryan | last post by:
I've created a custom configuration section that inherits (naturally) from System.Configuration.ConfigurationSection. The configuration section is working 99% fine, however I keep coming across a...
3
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I cut and paste the following code from msdn help page which it just introduces view and multiview server controls. Here is what I do: in vs studio 2005, File --New Web Site, it...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.