SoFunction
Updated on 2024-11-13

Python automated testing ConfigParser module reads and writes configuration files

Python automated testing ConfigParser module reads and writes configuration files

ConfigParser is Python's own module, used to read and write configuration files, the use of its simple. Directly on the code, do not explain, do not say.

The format of the configuration file is: [] contains what is called a section, and under section there are keys like option=value.

configuration file

[section1]
name = tank
age = 28

[section2]
ip = 192.168.1.1
port = 8080

Python code

# -* - coding: UTF-8 -* - 
import ConfigParser

conf = ()
("c:\\")

# Get the value of the specified section, specified option
name = ("section1", "name")
print(name)
age = ("section1", "age")
print age

# Get all the sections
sections = ()
print sections

# Write configuration file

# Update the value of the specified section, option
("section2", "port", "8081")

# Write to the specified section, add the value of the new option.
("section2", "IEPort", "80")

# Add a new section
conf.add_section("new_section")
("new_section", "new_option", "/tankxiao")

# Write back to configuration file
(open("c:\\","w"))

The above is the Python ConfigParser module to read and write configuration files to organize the information, follow-up to continue to add relevant information, thank you for your support of this site!