Python read-Write INI configuration file method Instance analysis

Source: Internet
Author: User
This example describes the Python read-write INI configuration file method. Share to everyone for your reference. The 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 (Rea Dwriteconffile.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 sect Ion in Allsections:return else:cf.add_section (section) readwriteconffile.writeconfigparser (CF) @stati    Cmethod 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

Hopefully 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.