configparser模块

用于生成和修改常见配置文档,当前模块的名称在python 3.x版本变更为configparser;

来看一个好多软件的常见文档格式如下

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes

[bitbucket.org]
User = hg

[topsecret.server.com]
Port = 50022
ForwardX11 = no

如果想用python生成一个这样的文档怎么做呢

#!/usr/bin/env python3.8
# __author: "smoke"
# date: 2021/1/21 下午10:10

import configparser

config = configparser.ConfigParser()    #生成文件配置的对象
config["DEFAULT"] = {'ServerAliveInterval':'45',
                     'Compression':'yes',
                     'CompressionLevel':'9'}

config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'    # mutates the parser
topsecret['ForwardX11'] = 'no'  #same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini','w') as configfile:
    config.write(configfile)

查看生成的example.ini配置文件

smoke@smoke-GS70-2PC-Stealth:~/PycharmProjects/pythonProject/lean_python$ cat example.ini 
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes

[bitbucket.org]
user = hg

[topsecret.server.com]
host port = 50022
forwardx11 = no

读配置文件

#!/usr/bin/env python3.8
# __author: "smoke"
# date: 2021/1/21 下午10:10

import configparser

config = configparser.ConfigParser()    #生成文件配置的对象

print(config.sections())    #读取节点名字以列表返回

/home/smoke/PycharmProjects/pythonProject/venv/bin/python /home/smoke/PycharmProjects/pythonProject/lean_python/configparser_module.py
[]

Process finished with exit code 0

#!/usr/bin/env python3.8
# __author: "smoke"
# date: 2021/1/21 下午10:10

import configparser

config = configparser.ConfigParser()    #生成文件配置的对象

config.read('example.ini')  #读取配置文件,做关联
print(config.sections())    #读取节点名字以列表返回,不会返回DEFAULT

/home/smoke/PycharmProjects/pythonProject/venv/bin/python /home/smoke/PycharmProjects/pythonProject/lean_python/configparser_module.py
['bitbucket.org', 'topsecret.server.com']

Process finished with exit code 0

#!/usr/bin/env python3.8
# __author: "smoke"
# date: 2021/1/21 下午10:10

import configparser

config = configparser.ConfigParser()    #生成文件配置的对象

config.read('example.ini')  #读取配置文件,做关联
print(config.defaults())    #读取defaults节点键值,

/home/smoke/PycharmProjects/pythonProject/venv/bin/python /home/smoke/PycharmProjects/pythonProject/lean_python/configparser_module.py
{'serveraliveinterval': '45', 'compression': 'yes', 'compressionlevel': '9', 'forwardx11': 'yes'}

Process finished with exit code 0

#!/usr/bin/env python3.8
# __author: "smoke"
# date: 2021/1/21 下午10:10

import configparser

config = configparser.ConfigParser()    #生成文件配置的对象

config.read('example.ini')  #读取配置文件,做关联

print('bitbucket.org' in config)    #判断config有没有bitbucket.org配置块

/home/smoke/PycharmProjects/pythonProject/venv/bin/python /home/smoke/PycharmProjects/pythonProject/lean_python/configparser_module.py
True

Process finished with exit code 0

#!/usr/bin/env python3.8
# __author: "smoke"
# date: 2021/1/21 下午10:10

import configparser

config = configparser.ConfigParser()    #生成文件配置的对象

config.read('example.ini')  #读取配置文件,做关联

print('bytebong.com' in config)

/home/smoke/PycharmProjects/pythonProject/venv/bin/python /home/smoke/PycharmProjects/pythonProject/lean_python/configparser_module.py
False

Process finished with exit code 0

#!/usr/bin/env python3.8
# __author: "smoke"
# date: 2021/1/21 下午10:10

import configparser

config = configparser.ConfigParser()    #生成文件配置的对象

config.read('example.ini')  #读取配置文件,做关联
print(config['bitbucket.org']['User'])  #取得User的值

/home/smoke/PycharmProjects/pythonProject/venv/bin/python /home/smoke/PycharmProjects/pythonProject/lean_python/configparser_module.py
hg

Process finished with exit code 0

#!/usr/bin/env python3.8
# __author: "smoke"
# date: 2021/1/21 下午10:10

import configparser

config = configparser.ConfigParser()    #生成文件配置的对象
config.read('example.ini')  #读取配置文件,做关联

topsecret = config['topsecret.server.com']

print(topsecret['Forwardx11'])

/home/smoke/PycharmProjects/pythonProject/venv/bin/python /home/smoke/PycharmProjects/pythonProject/lean_python/configparser_module.py
no

Process finished with exit code 0

#!/usr/bin/env python3.8
# __author: "smoke"
# date: 2021/1/21 下午10:10

import configparser

config = configparser.ConfigParser()    #生成文件配置的对象
config.read('example.ini')  #读取配置文件,做关联

for key in config:
    print(key)    #会打印所有的块

/home/smoke/PycharmProjects/pythonProject/venv/bin/python /home/smoke/PycharmProjects/pythonProject/lean_python/configparser_module.py
DEFAULT
bitbucket.org
topsecret.server.com

Process finished with exit code 0

#!/usr/bin/env python3.8
# __author: "smoke"
# date: 2021/1/21 下午10:10

import configparser

config = configparser.ConfigParser()    #生成文件配置的对象
config.read('example.ini')  #读取配置文件,做关联

for key in config['bitbucket.org']: #会打印default和bitbucket.org的节点所有子键
    print(key)

/home/smoke/PycharmProjects/pythonProject/venv/bin/python /home/smoke/PycharmProjects/pythonProject/lean_python/configparser_module.py
user
serveraliveinterval
compression
compressionlevel
forwardx11

Process finished with exit code 0

删除配置文件内容

#!/usr/bin/env python3.8
# __author: "smoke"
# date: 2021/1/21 下午10:10

import configparser

config = configparser.ConfigParser()    #生成文件配置的对象
config.read('example.ini')  #读取配置文件,做关联

config.remove_section('topsecret.server.com')   #删除topsecret.server.com节点所有内容
config.write(open('i.cfg',"w")) #重新创建一个新文件写入

/home/smoke/PycharmProjects/pythonProject/venv/bin/python /home/smoke/PycharmProjects/pythonProject/lean_python/configparser_module.py

Process finished with exit code 0

查看原配置文件和删除后的配置文件

smoke@smoke-GS70-2PC-Stealth:~/PycharmProjects/pythonProject/lean_python$ cat example.ini 
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes

[bitbucket.org]
user = hg

[topsecret.server.com]
host port = 50022
forwardx11 = no
smoke@smoke-GS70-2PC-Stealth:~/PycharmProjects/pythonProject/lean_python$ cat i.cfg 
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes

[bitbucket.org]
user = hg

删除配置文件内容覆盖原文件内容

#!/usr/bin/env python3.8
# __author: "smoke"
# date: 2021/1/21 下午10:10

import configparser

config = configparser.ConfigParser()    #生成文件配置的对象
config.read('example.ini')  #读取配置文件,做关联

config.remove_section('topsecret.server.com')   #删除topsecret.server.com节点所有内容
config.write(open('example.ini',"w")) #重新创建一个新文件写入

/home/smoke/PycharmProjects/pythonProject/venv/bin/python /home/smoke/PycharmProjects/pythonProject/lean_python/configparser_module.py

Process finished with exit code 0

查看原配置文件

smoke@smoke-GS70-2PC-Stealth:~/PycharmProjects/pythonProject/lean_python$ cat example.ini 
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes

[bitbucket.org]
user = hg

读配置文件

#!/usr/bin/env python3.8
# __author: "smoke"
# date: 2021/1/21 下午10:10

import configparser

config = configparser.ConfigParser()    #生成文件配置的对象
config.read('example.ini')  #读取配置文件,做关联

print(config.has_section('topsecret.server.com'))   #判断配置文件有没有topsecret.server.com字符串

/home/smoke/PycharmProjects/pythonProject/venv/bin/python /home/smoke/PycharmProjects/pythonProject/lean_python/configparser_module.py
False

Process finished with exit code 0

修改配置文件

#!/usr/bin/env python3.8
# __author: "smoke"
# date: 2021/1/21 下午10:10

import configparser

config = configparser.ConfigParser()    #生成文件配置的对象
config.read('example.ini')  #读取配置文件,做关联

config.set('bitbucket.org','user','smoke')   #修改健的值
config.write(open('example.ini',"w"))

/home/smoke/PycharmProjects/pythonProject/venv/bin/python /home/smoke/PycharmProjects/pythonProject/lean_python/configparser_module.py

Process finished with exit code 0

查看原配置文件

smoke@smoke-GS70-2PC-Stealth:~/PycharmProjects/pythonProject/lean_python$ cat example.ini 
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes

[bitbucket.org]
user = smoke

删除配置文件内容 

#!/usr/bin/env python3.8
# __author: "smoke"
# date: 2021/1/21 下午10:10

import configparser

config = configparser.ConfigParser()    #生成文件配置的对象
config.read('example.ini')  #读取配置文件,做关联

config.remove_option('bitbucket.org','user')    #删除主健下的子健值
config.write(open('example.ini',"w"))

/home/smoke/PycharmProjects/pythonProject/venv/bin/python /home/smoke/PycharmProjects/pythonProject/lean_python/configparser_module.py

Process finished with exit code 0

查看原配置文件

smoke@smoke-GS70-2PC-Stealth:~/PycharmProjects/pythonProject/lean_python$ cat example.ini 
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes

[bitbucket.org]