python中读取配置文件的方式
方式1:argparse
argparse,是Python标准库中推荐使用的编写命令行程序的工具。也可以用于读取配置文件。
字典样式的配置文件*.conf
配置文件test1.conf
{
"game0":
{
"ip":"127.0.0.1",
"port":27182,
"type":1
},
"game1":
{
"ip":"127.0.0.1",
"port":27183,
"type":0
},
"game2":
{
"ip":"127.0.0.1",
"port":27184,
"type":0
}
}
config.py
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: mytest.py
Description :
Author : andy9468
date: 2018/02/27
Copyright: (c) andy9468 2018
-------------------------------------------------
Change Activity:
2018/02/27:
-------------------------------------------------
"""
import json
import sys
import argparse def parse_args(args):
parser = argparse.ArgumentParser(prog="GameServer")
parser.add_argument('configfile', nargs=1, type=str, help='')
parser.add_argument('--game', default="game", type=str, help='')
return parser.parse_args(args) def parse(filename):
configfile = open(filename)
jsonconfig = json.load(configfile)
configfile.close()
return jsonconfig def main(argv):
args = parse_args(argv[1:])
print("args:", args)
config = parse(args.configfile[0])
info = config[args.game]
_ip = info['ip']
_port = info['port']
print("type:", type(_port))
_type = info['type']
print("print:%s,%d,%d" % (_ip, _port, _type)) if __name__ == '__main__':
main(sys.argv)
运行
启动脚本:python test.py test.conf --game=game0
详见:
http://blog.csdn.net/majianfei1023/article/details/49954705
方式2:ConfigParser
ConfigParser是Python读取conf配置文件标准的库。
中括号下设置子项的配置文件*.conf、或者*.ini
test2.conf
[game0]
ip = 127.0.0.1
port = 27182
type = 1 [game1]
ip = 127.0.0.1
port = 27183
type = 0 [game2]
ip = 127.0.0.1
port = 27184
type = 0
test2.py
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: test2.py
Description :
Author : andy9468
date: 2018/02/27
Copyright: (c) andy9468 2018
-------------------------------------------------
Change Activity:
2018/02/27:
-------------------------------------------------
"""
# -*- coding:utf-8 -*- import configparser
import sys def parse_args(filename):
cf = configparser.ConfigParser()
cf.read(filename) # return all sections
secs = cf.sections()
print("sections:", secs) # game0 section
game0 = cf.options("game0")
print("game0:", game0) items = cf.items("game0")
print("game0 items:", items) # read
_ip = cf.get("game0", "ip")
_port = cf.getint("game0", "port")
_type = cf.getint("game0", "type")
print("print:%s,%d,%d" % (_ip, _port, _type)) def main(argv):
parse_args(argv[1]) if __name__ == '__main__':
main(sys.argv)
print(sys.argv)
动态添加配置:
# add
cf.add_section('test3')
cf.set('test3','id','123')
cf.write(open(filename,'w'))
详见:
https://www.cnblogs.com/emily-qin/p/8022292.html
方式3:用变量(常量)作为配置文件格式。*.py
配置文件:config.py
LISTEN_PORT = 4444
USE_EPOLL = True
导入配置:myread.py
import config
port_num = config.LISTEN_PORT
if config.USE_EPOLL:
print(config.USE_EPOLL)
详见:
http://www.pythontip.com/blog/post/4912/
python中读取配置文件的方式的更多相关文章
- python中读取配置文件ConfigParser
在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单的做一些介 ...
- 使用python中读取配置文件
最近在接触利用python来写测试框架,本人也是个刚接触python,所以是个小菜鸟,今天开始,一点点的记录学习中的积累,方便以后的学习以及回顾,也希望能帮助跟我一样的小菜鸟们一步步的成长起来.那么, ...
- python读取配置文件的方式
python读取配置文件的方式 1.从config.ini中读取,后缀无所谓,文件名字也无所谓,不过config.ini是常用写法,所谓见名知意 config.ini内容: [global] ip = ...
- java中读取配置文件ResourceBundle和Properties两种方式比较
今天在开发的时候,需要把一些信息放到配置文件中,方便后续的修改,注意到用的是ResourceBundle读取配置文件的方式,记得之前也见过使用Properties的方式,就比较好奇这两种方式的区别,网 ...
- JavaWeb中servlet读取配置文件的方式
我们在JavaWeb中常常要涉及到一些文件的操作,比如读取配置文件,下载图片等等操作.那我们能不能采用我们以前在Java工程中读取文件的方式呢?废话不多说我们来看看下我们以前在Java工程中读取文件是 ...
- Java读取配置文件的方式
Java读取配置文件的方式-笔记 1 取当前启动文件夹下的配置文件 一般来讲启动java程序的时候.在启动的文件夹下会有配置文件 classLoader.getResource(&qu ...
- .NET Core类库中读取配置文件
最近在开发基于.NET Core的NuGet包,遇到一个问题:.NET Core中已经没有ConfigurationManager类,在类库中无法像.NET Framework那样读取App.conf ...
- python ConfigParser读取配置文件,及解决报错(去掉BOM)ConfigParser.MissingSectionHeaderError: File contains no section headers的方法
先说一下在读取配置文件时报错的问题--ConfigParser.MissingSectionHeaderError: File contains no section headers 问题描述: 在练 ...
- 关于Python中读取写入文件并进行文件与用户交互的操作
一.提前知识点 在python中是同样和其他语言一样可以进行文件的读取写入操作,值得注意的是,Python中打开文件读取的方式有几种,分别是以下几种: f = open('username.txt') ...
随机推荐
- Spring-MVC案例:Spitter的笔记
源码地址:https://github.com/Young4Dream/yan/tree/master/Maven_spittr 笔记: 1.当DispatcherServlet启动时,会创建Spri ...
- eclipse 远程链接访问hadoop 集群日志信息没有输出的问题l
Eclipse插件Run on Hadoop没有用到hadoop集群节点的问题参考来源 http://f.dataguru.cn/thread-250980-1-1.html http://f.dat ...
- linux shell判断脚本执行时传入了多少个参数,判断脚本参数个数,参数数量
需求描述: 在写shell脚本的时候,会有这样的需求,脚本在执行的过程中是需要传入参数的,如果没有参数传递给脚本, 就会给出相应的脚本使用方法或者例子,告知,脚本后面应该加什么样的参数.所以呢,这里要 ...
- mysql数据库中,flush logs语句的作用是什么呢?
需求描述: 今天在研究mysql数据库的备份和恢复,用到了flush logs这个SQL语句. 所以,在此进行测试,并且记录该SQL语句的作用. 概念描述: 在mysql数据库,如果数据库启动的时候, ...
- javascript测试框架 Mocha 实例教程
http://www.ruanyifeng.com/blog/2015/12/a-mocha-tutorial-of-examples.html
- tiny4412 ubuntudesktop更新源(old)
1.报错:404 Not Found [IP: 91.189.88.151 80] 2. deb http://old-releases.ubuntu.com/ubuntu/ raring main ...
- AsyncTask应用示例
package com.example.testdemo; import java.io.ByteArrayOutputStream; import java.io.IOException; impo ...
- 查看系统负载:uptime
uptime命令用于查看系统负载,跟 w 命令的输出内容一致 [root@localhost ~]$ uptime :: up days, :, users, load average: 0.03, ...
- oracle SUM函数
select change_type as change_type, sum(points1) as points from (select DECODE(p.change_type, , ' 兑换商 ...
- vue中的小踩坑(01)
前言: 昨天算是使用vue2.0+element-ui做了一点小小的页面,可是源于其中遇到的问题,特地整理一下,以防自己还有其他的小伙伴们继续踩坑. 过程: 1.不知道大家有没有注意到 ...