方式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中读取配置文件的方式的更多相关文章

  1. python中读取配置文件ConfigParser

    在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单的做一些介 ...

  2. 使用python中读取配置文件

    最近在接触利用python来写测试框架,本人也是个刚接触python,所以是个小菜鸟,今天开始,一点点的记录学习中的积累,方便以后的学习以及回顾,也希望能帮助跟我一样的小菜鸟们一步步的成长起来.那么, ...

  3. python读取配置文件的方式

    python读取配置文件的方式 1.从config.ini中读取,后缀无所谓,文件名字也无所谓,不过config.ini是常用写法,所谓见名知意 config.ini内容: [global] ip = ...

  4. java中读取配置文件ResourceBundle和Properties两种方式比较

    今天在开发的时候,需要把一些信息放到配置文件中,方便后续的修改,注意到用的是ResourceBundle读取配置文件的方式,记得之前也见过使用Properties的方式,就比较好奇这两种方式的区别,网 ...

  5. JavaWeb中servlet读取配置文件的方式

    我们在JavaWeb中常常要涉及到一些文件的操作,比如读取配置文件,下载图片等等操作.那我们能不能采用我们以前在Java工程中读取文件的方式呢?废话不多说我们来看看下我们以前在Java工程中读取文件是 ...

  6. Java读取配置文件的方式

    Java读取配置文件的方式-笔记 1       取当前启动文件夹下的配置文件   一般来讲启动java程序的时候.在启动的文件夹下会有配置文件 classLoader.getResource(&qu ...

  7. .NET Core类库中读取配置文件

    最近在开发基于.NET Core的NuGet包,遇到一个问题:.NET Core中已经没有ConfigurationManager类,在类库中无法像.NET Framework那样读取App.conf ...

  8. python ConfigParser读取配置文件,及解决报错(去掉BOM)ConfigParser.MissingSectionHeaderError: File contains no section headers的方法

    先说一下在读取配置文件时报错的问题--ConfigParser.MissingSectionHeaderError: File contains no section headers 问题描述: 在练 ...

  9. 关于Python中读取写入文件并进行文件与用户交互的操作

    一.提前知识点 在python中是同样和其他语言一样可以进行文件的读取写入操作,值得注意的是,Python中打开文件读取的方式有几种,分别是以下几种: f = open('username.txt') ...

随机推荐

  1. Adobe Acrobat 不能打开在线pdf。Adobe Acrobat 应用程序正在被终止,因为内存错误

    Adobe Acrobat 应用程序正在被终止,因为内存错误. Adobe Acrobat 不能打开在线pdf. 当出现上面两种错误时. 原因可能是Acrobat的更新有问题. 解决方法:打开C:\D ...

  2. 超全面的JavaWeb笔记day19<Service>

    今日内容 l Service事务 l 客户关系管理系统 Service事务 在Service中使用ThreadLocal来完成事务,为将来学习Spring事务打基础! 1 DAO中的事务 在DAO中处 ...

  3. ArcGIS ArcPy Python处理数据

    1.使用搜索游标查看行中的字段值.import arcpy # Set the workspace arcpy.env.workspace = "c:/base/data.gdb" ...

  4. mqtt 服务器与客户端通讯

    mqtt 服务器与客户端通讯. 服务器端 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...

  5. php 连接mongdb的类

    <?php/** * php 连接mongdb的类的封装 * @author 李秀然 */ class mongdb{ private $host;//"mongodb://admin ...

  6. 基于Cocos2d-x学习OpenGL ES 2.0系列——初识MVP(3)

    在上一篇文章中,我在介绍vertex shader的时候挖了一个坑:CC_MVPMatrix.它其实是一个uniform,每一个Cocos2d-x预定义的shader都包含有这个uniform,但是如 ...

  7. js禁止img拖动

    其实只需要一句代码即可,那就是阻止元素的默认事件: <body> <img src="./../imgs/cat.jpg" id="test" ...

  8. java高级---->Thread之FutureTask的使用

    FutureTask类是Future 的一个实现,并实现了Runnable,所以可通过Excutor(线程池) 来执行,也可传递给Thread对象执行.今天我们通过实例来学习一下FutureTask的 ...

  9. Go基础---->go的基础学习(五)

    这里是go中关于io的一些知识.有时不是你装得天衣无缝,而是我愿意陪你演得完美无缺. go中关于io的使用 一.Reader中的Read方法 Read 用数据填充指定的字节 slice,并且返回填充的 ...

  10. 设计模式之抽象工厂模式(Java实现)

    “上次是我的不对,贿赂作者让我先讲来着,不过老婆大人大人有大量,不与我计较,这次还让我先把上次未讲完的应用场景部分给补充上去,有妻如此,夫复何求.”(说完,摸了摸跪的发疼的膝盖,咳咳,我发四我没笑!真 ...