Configparser module of the Python module

Source: Internet
Author: User

Configparser module: for creating profiles in a certain format

Create

ImportConfigparserconfig=Configparser. Configparser () config['DEFAULT'] = {'default':'Yes'}config['Path'] = {'UserInfo': R'E:\pycharm\ Learning \day29\userinfo'}with Open ('Userinfo.ini','W', encoding='Utf-8') as F:config.write (f)

After the file is completed:

== E:\pycharm\ Learning \day29\userinfo

View

Print(Config.sections ())#[] Because the file has not yet been readConfig.read ('Userinfo.ini', encoding='Utf-8')Print(Config.sections ())#[' path '] read out node [' Default '] for default node not printingPrint('Path' inchConfig#True to determine if a node is in the configuration filePrint(config['Path']['UserInfo'])#E:\pycharm\ Learning \day29\userinfo The configuration item under the Read node no such target errorPrint(Config.get ('Path','UserInfo'))#E:\pycharm\ Learning \day29\userinfo forKinchconfig['Path']:#Printing the configuration items under the ' Path ' node also prints all the items under the default node    Print(k)#userinfo DefaultPrint(Config.items ('Path'))#[(' Default ', ' Yes '), (' UserInfo ', ' e:\\pycharm\\ Learning \\day29\\userinfo ')]

Increase

Config.read ('Userinfo.ini', encoding='Utf-8') config.add_section ('IP')#Adding nodesPrint(Config.sections ())#[' Path ', ' IP ']Config.set ('IP','IP','192.168.1.1')#Add a configuration item to a nodeConfig.set ('Path','UserInfo','None')#Modifying configuration ItemsPrint(config['IP']['IP'])#192.168.1.1Print(config['Path']['UserInfo'])#NoneConfig.write (Open ('Userinfo.ini','W', encoding='Utf-8'))#to re-write the modifications back to the file

Delete

Config.read ('Userinfo.ini', encoding='Utf-8')Print(Config.sections ())#[' Path ', ' IP ']Config.remove_section ('IP')#Delete a nodePrint(Config.sections ())#[' Path ']Print(Config.items ('Path'))#[(' Default ', ' Yes '), (' UserInfo ', ' None ')]Config.remove_option ('Path','UserInfo')#to delete a configuration item in a nodePrint(Config.items ('Path'))#[(' Default ', ' Yes ')]Config.write (Open ('Userinfo.ini','W', encoding='Utf-8'))#to re-write the modifications back to the file

Configparser module of the Python module

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.