Writing file using configparser

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

    Writing file using configparser

    According to what I read in the ConfigParser module, this should work, but it doesn't. Can anyone point out what I'm doing wrong?

    Code:
        def writeinifile ( self, FILE='P:/INI/test.ini', SECTION='Testsection', OPTION='Item', VALUE='wheeee' ):
            p = ConfigParser()
            p.read(FILE)
            if not p.has_section ( SECTION ):
                p.add_section(SECTION)
            p.set(SECTION, OPTION, VALUE)
            p.write(FILE)
    The error I keep getting:
    Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win
    32
    Type "help", "copyright" , "credits" or "license" for more information.
    >>> from INIfile import INI
    >>> I = INI()
    >>> I.writeinifile ( SECTION='Sectio n' )
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "C:\Python25\li b\INIfile.py", line 23, in writeinifile
    p.write(fp=FILE )
    File "C:\Python25\li b\ConfigParser. py", line 369, in write
    fp.write("[%s]\n" % section)
    AttributeError: 'str' object has no attribute 'write'
    >>>
    To me it looks like it is something wrong in the ConfigParser code, But I'm sure thats just foolish arrogance getting to me, because I of course keep insisting I'm right. :)
  • William Manley
    New Member
    • Mar 2007
    • 56

    #2
    Oops, sorry, Realised I didn't post the entire code.
    Code:
    from ConfigParser import ConfigParser
    
    class INI:
        def getinifile ( self, FILE, SECTION, OPTION, DEFAULT ):
            p = ConfigParser()
            p.read(FILE)
            if p.has_section ( SECTION ):
                #print 'Has Section'
                if p.has_option ( SECTION, OPTION ):
                    #print 'Has Option'
                    Value = p.get ( SECTION, OPTION ).strip()
                else:
                    Value = DEFAULT
            else:
                Value = DEFAULT
            return Value
        def writeinifile ( self, FILE='P:/INI/test.ini', SECTION='Testsection', OPTION='Item', VALUE='wheeee' ):
            p = ConfigParser()
            p.read(FILE)
            if not p.has_section ( SECTION ):
                p.add_section(SECTION)
            p.set(SECTION, OPTION, VALUE)
            p.write(FILE)

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by William Manley
      Oops, sorry, Realised I didn't post the entire code.
      Code:
      from ConfigParser import ConfigParser
      
      class INI:
          def getinifile ( self, FILE, SECTION, OPTION, DEFAULT ):
              p = ConfigParser()
              p.read(FILE)
              if p.has_section ( SECTION ):
                  #print 'Has Section'
                  if p.has_option ( SECTION, OPTION ):
                      #print 'Has Option'
                      Value = p.get ( SECTION, OPTION ).strip()
                  else:
                      Value = DEFAULT
              else:
                  Value = DEFAULT
              return Value
          def writeinifile ( self, FILE='P:/INI/test.ini', SECTION='Testsection', OPTION='Item', VALUE='wheeee' ):
              p = ConfigParser()
              p.read(FILE)
              if not p.has_section ( SECTION ):
                  p.add_section(SECTION)
              p.set(SECTION, OPTION, VALUE)
              p.write(FILE)
      William,
      Would you also post your file 'test.ini'? That would help. Thanks.

      Comment

      • ghostdog74
        Recognized Expert Contributor
        • Apr 2006
        • 511

        #4
        Originally posted by William Manley
        According to what I read in the ConfigParser module, this should work, but it doesn't. Can anyone point out what I'm doing wrong?

        Code:
            def writeinifile ( self, FILE='P:/INI/test.ini', SECTION='Testsection', OPTION='Item', VALUE='wheeee' ):
                p = ConfigParser()
                p.read(FILE)
                if not p.has_section ( SECTION ):
                    p.add_section(SECTION)
                p.set(SECTION, OPTION, VALUE)
                p.write(FILE)
        The error I keep getting:


        To me it looks like it is something wrong in the ConfigParser code, But I'm sure thats just foolish arrogance getting to me, because I of course keep insisting I'm right. :)
        a first look at the code, the FILE parameter is defined as a string. and according to the docs, the write() method argument is supposed to be a file object. I suppose you need to use a file pointer instead. something like this when you initialise a file object pointer.
        Code:
        f = open("file","w")  # f is a file object now.

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by ghostdog74
          a first look at the code, the FILE parameter is defined as a string. and according to the docs, the write() method argument is supposed to be a file object. I suppose you need to use a file pointer instead. something like this when you initialise a file object pointer.
          Code:
          f = open("file","w")  # f is a file object now.
          ghostdog74 is correct as usual! I played with it some:
          Code:
          >>> fp = open(r'H:\TEMP\temsys\TEST.INI')
          >>> p = ConfigParser()
          >>> p.readfp(fp)
          >>> p.set('X Axis', 'usage', '0x90')
          >>> p.items('X Axis')
          [('name', 'X AXIS'), ('linedata.1', '(310,60),(310,76),(268,76)'), ('usage', '0x90'), ('calloutmax.1', '(296,17),(386,59)'), ('type', '0x2'), ('usagepage', '0x1'), ('align.1', 'BL')]
          >>> p.write(open(r'H:\TEMP\temsys\TEST1.INI', 'w'))
          >>>

          Comment

          • ghostdog74
            Recognized Expert Contributor
            • Apr 2006
            • 511

            #6
            Originally posted by bvdet
            ghostdog74 is correct as usual! I played with it some:
            Code:
            >>> fp = open(r'H:\TEMP\temsys\TEST.INI')
            >>> p = ConfigParser()
            >>> p.readfp(fp)
            >>> p.set('X Axis', 'usage', '0x90')
            >>> p.items('X Axis')
            [('name', 'X AXIS'), ('linedata.1', '(310,60),(310,76),(268,76)'), ('usage', '0x90'), ('calloutmax.1', '(296,17),(386,59)'), ('type', '0x2'), ('usagepage', '0x1'), ('align.1', 'BL')]
            >>> p.write(open(r'H:\TEMP\temsys\TEST1.INI', 'w'))
            >>>
            thanks for verifying :-).

            Comment

            • William Manley
              New Member
              • Mar 2007
              • 56

              #7
              Wow, I love you guys, thanks. It works great. :)

              Comment

              Working...