I. Configparser module
Used to generate and modify common configuration documents, but the Money module name was changed to Configparser in the python3.x version.
1. Generate a configuration.
ImportConfigparserconfig=Configparser. Configparser () config["DEFAULT"] = {'Serveraliveinterval':' $','Compression':'Yes','CompressionLevel':'9'}config['bitbucket.org'] ={}config['bitbucket.org']['User'] ='HG'With Open ('Example.ini','W') as Configfile:config.write (configfile)
Note: Generate Configuration file Example.ini
2. Read the configuration file
Import= configparser. Configparser () Conf.read("example.ini")print( Conf.defaults ())print(conf.sections ())print(conf[' bitbucket.org'['user'])
Note: conf.defaults: Read defaults read in dictionary type
Note: conf.sections: Reads a node and does not contain defaults.
Note: conf[' bitbucket.org '[' user ']: reads the contents of the node directly.
4. Delete the configuration file contents.
Importconfigparserconf=Configparser. Configparser () Conf.read ("Example.ini")Print(Conf.defaults ())Print(Conf.sections ())Print(conf['bitbucket.org']['User']) SEC= Conf.remove_section ('bitbucket.org') conf.write (open ('exmple2.cfg',"W"))
Note: Delete and create a backup within the new file.
Python Configparser Module