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') ...
随机推荐
- ftp命令行工具如何 连接 非标准21端口(其他端口)的ftp服务器
windows: step1:ftp命令进入ftp交互环境 step2:ftp>open ip空格port 然后...
- hadoop3.1.0 window win7 基础环境搭建
https://blog.csdn.net/wsh596823919/article/details/80774805 hadoop3.1.0 window win7 基础环境搭建 前言:在windo ...
- ModelState.AddModelError使用
后台: ModelState.AddModelError("userPwd", "请输入密码!"); ModelState是一个字典类型,这句话的作用是向Mod ...
- BaiduMap 鼠标绘制矩形选框四个顶角坐标的获取
雪影工作室版权全部.转载请注明[http://blog.csdn.net/lina791211] 1.博文产生原因 在使用百度Map开放API进行开发的时候,遇到了一个需求,非常easy的一个需求. ...
- 京东云擎”本周四推出一键免费安装Discuz论坛
“京东云擎”本周四推出一键免费安装Discuz论坛了,让用户能在1分钟之内建立自己的论坛.这是继上周云擎推出一键安装WordPress之后的又一重大免费贡献! 云擎: http://jae.jd.co ...
- C# string.Format("{0:C3}", 2)
- linux系统UDP的socket通信编程
发送方: /* * File: main.c * Author: tianshuai * * Created on 2011年11月29日, 下午10:34 * * 主要实现:发送20个文本消息,然后 ...
- JS基本动画
<style type="text/css"> .color_red { background: red; } div { position: absolute; to ...
- poj_2286 IDA*
题目大意 给定一个由数字组成的#字型网格,和一定的移动规则,问最少需要多少次移动才能达到要求的结果. 题目分析 要求最少需要几步到达结果,可以考虑广度优先搜索算法,或者迭代加深深度优先搜索(IDA*) ...
- ubuntu 14 root 账户 启用与ssh登录
ubuntu 14.04 root用户登录 开启root帐号的方法: 为了启用root 帐号(也就是设置一个口令)使用: sudo passwd root 当你使用完毕后屏蔽root帐号使用: sud ...