Python encapsulation configparser module get Conf.ini value

Source: Internet
Author: User

Configparser module is a python from the file to get the fixed format parameters of the module, because it is python only, we should use a lot of, I think this parameter module is more flexible, add parameters, modify parameters, read parameters, etc. have corresponding parameters for the user to use. Because I see is close to the automation framework, the main use of reading parameters, manual operation to add and modify parameters may be more convenient.

The parameters read by the Configparser module should be in an unlimited document format: *.ini, *.conf, *. *, but the content format should be filled in a fixed format:

[Section]

Key = value

1 #路径参数2 [path]3My_dir =D:\python\workspace\pythontest4Log_path =%(my_dir) s\\log\5Img_path =%(my_dir) s\screenshot6Report_path =%(my_dir) s\\report\7 8 #邮箱参数9 [email]TenUESR =1234567890 OnePassword =123456 ASMTPHost = [email protected]163. com -  from=123123@163. com -to =[email protected] the  - #日志参数 - [log] -LogTime =112323 +  - #测试参数 + [main] AIP =109.132.19.7 atPort =9900 -url = http://% (IP) s:% (port) s/tes

Post-packaged code:

1 #Coding:utf-82 ImportConfigparser3 ImportOS4 5 classReadconfig (object):6     " "Configuration Parameters" "7     def __init__(self):8         " "9 Usage:Ten conf = readconfig () One confinfo = conf.getconf ("url") A         " " -         #Set Conf.ini path -Current_dir = Os.path.dirname (__file__) theTop_one_dir =os.path.dirname (Current_dir) -file_name = Top_one_dir +"\\conf\\conf.ini" -         #instantiating a Configparser object -Self.config =Configparser.configparser () + Self.config.read (file_name) -  +     defgetconf (Self, keyname): Anum =Len (self.config.sections ()) ati =0 -         Print("num =", num) -         #cycle through the number of sections to find out if KeyName exists -          whileI <Num: -             #Each loop gets the section in the corresponding list -Section =self.config.sections () [i] in             #if KeyName exists in the section, its parameter values are output -             ifKeyNameinchself.config.options (section): to                 Pass +                 Print("Num --", i) -                 Print("Section --->", section) the                 Print("KeyName--->", KeyName) *Self.info =self.config.get (section, KeyName) $                  BreakPanax Notoginseng             Else : -i = i + 1 the         returnSelf.info +  A if __name__=="__main__": theconf =Readconfig () +Confinfo = conf.getconf ("URL") -     Print("Config--->", Confinfo)

At first, I wanted to get the corresponding key and value through the section value, but I tried it many times without success. Original idea:

1 conf = readconfig ()2 confinfo = conf.getconf ("main"). URL

Main is section,url as key.

But this method of getting the parameter value by key can also accomplish my purpose.

1 conf = readconfig ()2 confinfo = conf.getconf ("url")

URL is key

Operation Result:

INI file is stored under pythontest/conf

The encapsulated py file is stored under Pythontest/commlib

Python encapsulation configparser module get Conf.ini value

Related Article

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.