Python read/write ini configuration file method instance analysis

Source: Internet
Author: User
This article mainly introduces how to read and write the ini configuration file in python. The example analyzes the reading and writing skills related to the ini configuration file in Python, for more information about how to read and write the ini configuration file in python, see the following example. Share it with you for your reference. The specific implementation method is as follows:

import ConfigParserimport osclass ReadWriteConfFile:  currentDir=os.path.dirname(__file__)   filepath=currentDir+os.path.sep+"inetMsgConfigure.ini"  @staticmethod  def getConfigParser():    cf=ConfigParser.ConfigParser()    cf.read(ReadWriteConfFile.filepath)    return cf  @staticmethod  def writeConfigParser(cf):    f=open(ReadWriteConfFile.filepath,"w");          cf.write(f)    f.close();  @staticmethod  def getSectionValue(section,key):    cf=ReadWriteConfFile.getConfigParser()    return cf.get(section, key)  @staticmethod  def addSection(section):    cf=ReadWriteConfFile.getConfigParser()    allSections=cf.sections()    if section in allSections:      return    else:      cf.add_section(section)      ReadWriteConfFile.writeConfigParser(cf)  @staticmethod  def setSectionValue(section,key,value):    cf=ReadWriteConfFile.getConfigParser()    cf.set(section, key, value)    ReadWriteConfFile.writeConfigParser(cf)if __name__ == '__main__':  ReadWriteConfFile.addSection( 'messages')  ReadWriteConfFile.setSectionValue( 'messages','name','sophia')  x=ReadWriteConfFile.getSectionValue( 'messages','1000')  print x

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.