config parser - modified

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • William Manley
    New Member
    • Mar 2007
    • 56

    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\li b\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:
    Code:
    from ConfigParser import ConfigParser
    class INI:
        def getinifile ( self, FILE, SECTION, ITEM, DEFAULT ):
            p = ConfigParser()
            p.read(FILE)
            Value = p.get ( SECTION, ITEM ).lstrip().rstrip()
            if len(Value) == 0:
                Value = DEFAULT
            return Value
    test.py
    Code:
    import INIfile
    
    INI = INIfile.INI()
    value = INI.getinifile ( 'P:/INI/test.ini', 'Testsection', 'Testoption', 'Default Value' )
    print value
    I just don't see what I'm doing wrong.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by William Manley
    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:
    Code:
    from ConfigParser import ConfigParser
    class INI:
        def getinifile ( self, FILE, SECTION, ITEM, DEFAULT ):
            p = ConfigParser()
            p.read(FILE)
            Value = p.get ( SECTION, ITEM ).lstrip().rstrip()
            if len(Value) == 0:
                Value = DEFAULT
            return Value
    test.py
    Code:
    import INIfile
    
    INI = INIfile.INI()
    value = INI.getinifile ( 'P:/INI/test.ini', 'Testsection', 'Testoption', 'Default Value' )
    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:
    Code:
    print p.sections()
    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().rs trip().

    Comment

    • William Manley
      New Member
      • Mar 2007
      • 56

      #3
      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.

      Comment

      • William Manley
        New Member
        • Mar 2007
        • 56

        #4
        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:\Documen ts and Settings\Willia m\Desktop\test. py", line 3, in <module>
        INI.writeinifil e ( 'P:/INI/test.ini', 'Testsection', 'Testoption2', 'koo koo ka choo' )
        File "C:\Python25\li b\INIfile.py", line 28, in writeinifile
        p.write(FILE)
        File "C:\Python25\li b\ConfigParser. py", line 369, in write
        fp.write("[%s]\n" % section)
        AttributeError: 'str' object has no attribute 'write'
        >>>
        test.py
        Code:
        import INIfile
        INI = INIfile.INI()
        INI.writeinifile ( 'P:/INI/test.ini', 'Testsection', 'Testoption2', 'koo koo ka choo' )
        INIfile.py
        Code:
        from ConfigParser import ConfigParser
        
        class INI:
            def getinifile ( self, FILE, SECTION, ITEM, DEFAULT ):
                p = ConfigParser()
                p.read(FILE)
                if p.has_section ( SECTION ):
                    print 'Has Section'
                    if p.has_option ( SECTION, ITEM ):
                        print 'Has Option'
                        Value = p.get ( SECTION, ITEM ).strip()
                    else:
                        Value = DEFAULT
                else:
                    Value = DEFAULT
                return Value
            def writeinifile ( self, FILE, SECTION, ITEM, VALUE ):
                p = ConfigParser()
                p.read(FILE)
                if p.has_section(SECTION):
                    pass
                else:
                    p.add_section(SECTION)
                if p.has_option(SECTION, ITEM):
                    pass
                else:
                    p.set(SECTION, ITEM, VALUE)
                p.write(FILE)

        Comment

        Working...