Application of Python Config.ini

Source: Internet
Author: User

The structure of the Config.ini file is the following: The structure is a section, a part of the structure under the "[]". There are three sections below, each of which is Section0,section1,section2

[MySQL config]Host=127.0.0.1Port=8080username=Rootpassword=123456[Online config]Online=Www.online.comusername=Peixmpassword=123qwe[Test config]Test=Www.test.comusername=Peixmpassword=123qwe

So, how do you get those things in your code?

First, you need to have a config.ini file

#!/usr/bin/env/python#-*-coding:utf-8-*-#Authour:xiapmin_peiImportConfigparser,os#encapsulates a path and directly enters the file name filename to get the path to filenamedefgetpath (filename):returnOs.path.join (Os.path.dirname (__file__), Os.pardir,'Data', filename)classConfig (object):def __init__(self,filename,section):""":p Aram FileName: File name:p Aram section: Part of the file, which is plastic"""self.section= Section#instantiate a Configparser objectSELF.CF =Configparser. Configparser ()#read the contents of a fileself.cf.read (GetPath (filename))defGetConfig (self,avg):"""get the content of the desired attribute:p Aram avg: Property Name: Return: The value of the property"""        Printself.cf.sections () parameter=Self.cf.get (Self.cf.sections () [Self.section],avg)returnparameterif __name__=="__main__":    #instantiate config, want to config.ini a file, 2nd section contentcon = Config ("Config.ini", 1)    #gets the value of the online property    PrintCon.getconfig ('Online')

Execution results: Thus, sections is a list

[' MySQL config ', ' online config ', ' Test config ']
Www.online.com

Process finished with exit code 0

Application of Python Config.ini

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.