Python configparser module

Source: Internet
Author: User

Configparser module for operating configuration files
A configuration file (INI file) consists of a section, a key, a value
1. Config=configparser.configparser () Create Configparser instance
2. Config.sections () returns the section sequence in the configuration file
3. Config.options (section) returns a sequence of all keys in a project
4, Config.get (section,option) returns the key value of option in section
5, Config.add_section (str) Add a configuration file node (str)
6, Config.set (section,option,val) Set the value of the key named option (Val) in the section node
7. Config.read (filename) Read the configuration file
8, Config.write (obj_file) write the configuration file

Case:
Import Configparser
Import OS
Class Readwriteconffile:
Currentdir=os.path.dirname (file) @staticmethod
br/>filepath=currentdir+os.path.sep+ "Inetmsgconfigure.ini"
@staticmethod
Cf=configparser.configparser ()
Cf.read (Readwriteconffile.filepath)
return cfbr/> @staticmethod
F=open (Readwriteconffile.filepath, "w"); Br/>cf.write (f)
F.close ();
@staticmethod
Cf=readwriteconffile.getconfigparser ()
return Cf.get (section, key) br/> @staticmethod
Cf=readwriteconffile.getconfigparser ()
Allsections=cf.sections ()
If clause in Allsections:br/>return
Else
Cf.add_section (section)
Readwriteconffile.writeconfigparser (CF)
@staticmethod
Cf=readwriteconffile.getconfigparser ()
Cf.set (section, key, value)
Readwriteconffile.writeconfigparser (CF)
Ifname= = 'Main‘:
Readwriteconffile.addsection (' Messages ')
Readwriteconffile.setsectionvalue (' Messages ', ' name ', ' Sophia ')
X=readwriteconffile.getsectionvalue (' Messages ', ' 1000 ')

Python configparser module

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.