Configuration file parser. A setup file consists of sections, lead by a "[section]" header, and followed by "name: value" entries, with continuations and such in the style ofRFC 822. 该模块支持读取类似如上格式的配置文件,如 windows 下的 .conf 及 .ini 文件等。 本章节使用如下的配置文件作...
parser.add_argument('--workernum', type = int, default = 1, help ='Worker process num') args=parser.parse_args()#Command-line argumentscontext.LISTEN_PORT =args.port context.CONFIG_FILE=args.conf workernum=args.workernum#Parse config fileconfig =ConfigParser.ConfigParser() config.read(context...
config.add_section(str) # 往配置文件中添加section config.set(section, name, value) # 在section下设置name=value config.remove_option(section, option) # 删除指定节点section中的option config.read(filename) # 读取配置文件, read(’./config.ini’) config.write(obj_file) # 写入配置文件 ,write(o...
Below, the parser is being used to get the database port. The getint() method is the preferred way. In addition, we can set a fallback parameter in case the value is not set in the config file, which is the case fordb_default_port. print(parser.get('db','db_port'),type(parser...
Configuration file parser. A setup file consists of sections, lead by a "[section]" header, and followed by "name: value" entries, with continuations and such in the style of RFC 822. 1. 2. 3. 4. 5. 该模块支持读取类似如上格式的配置文件,如 windows 下的 .conf 及 .ini 文件等。
reference: http://www.cnblogs.com/feeland/p/4514771.html Python 读取写入配置文件很方便,可使用内置的 configparser 模块;可查看源码,如博主本机地址: “C:/python27/lib/configparser.py” Configuration file parser ...
示例配置文件(config.ini): # 这是一个注释 [Database] host = localhost port = 5432 username = admin password = password123 ; 这也是一个注释 [Logging] level = INFO file = app.log // 这同样是一个注释 在读取配置文件时,注释行会被configparser模块自动忽略,不会包含在返回的配置信息中。
config = ConfigParser.ConfigParser() config.readfp(open('defaults.cfg')) config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')]) 1. 2. 3. 4. 5. cfgparser = ConfigParser() ... cfgparser.optionxform = str 1. 2. 3. ...
remove_option('add_node', 'test2') config.remove_section("add_node") config.write(open(ini_path, "w")) ConfigParser解析命令行参数 在上节我们提到了ConfigParser,并使用parser解析配置文件,对配置文件进行增删改。这节我们详细介绍ConfigParser如何解析命令行参数。 代码语言:javascript 代码运行次数:0 运行 ...
with open("demo.ini", "w") as configfile: config.write(configfile) 对配置解析器的处理与字典类似。 读取配置文件: >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read("demo.ini") ["demo.ini"] >>> config.sections() ...