configparser 操作配置文件模块
创建一个配置文件
import configparser config = configparser.ConfigParser() config["DEFAULT"] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9', 'ForwardX11':'yes' } config['bitbucket.org'] = {'User':'hg'} config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'} config['path'] = { 'Base_Path' : 'E:\s17\day28\周末作业讲解', 'students_path' :'E:\s17\day28\周末作业讲解\students' } with open('example.ini', 'w',encoding='utf-8') as configfile: config.write(configfile)
执行结果:
[DEFAULT] serveraliveinterval = 45 compression = yes compressionlevel = 9 forwardx11 = yes [bitbucket.org] user = hg [topsecret.server.com] host port = 50022 forwardx11 = no [path] base_path = E:\s17\day28\周末作业讲解 students_path = E:\s17\day28\周末作业讲解\students