方式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. 将android程序中的数据库导出到SD卡

    private void copyDBToSDcrad() { String DATABASE_NAME = "数据库文件名"; String oldPath = "da ...

  2. 批量快速的导入导出Oracle的数据(spool缓冲池、java实现)

    1. Java代码实现思路 BufferedWriter writefile = new BufferedWriter(new FileWriter(file));  writefile.write( ...

  3. Loadrunner C 编程_1

    就目前的了解.Loadrunner的脚本语言其实和C没什么区别.他内部的好多机制都是C实现的. 不过是一种“类C” 所以我从几个方面分析 1:定义常量变量和C一样 2:在LR中,C的变量和LR的参数是 ...

  4. C#字符串二进制互换

    static void Main(string[] args)         {             string str = "宋军辉";             Cons ...

  5. JavaScript jQuery 笔记

    资料来源:http://www.w3school.com.cn/jquery/index.asp http://files.cnblogs.com/files/defineconst/jQuery.r ...

  6. js方法区分IE浏览器和非IE浏览器

    可以从IE特有的方法和非IE特有的方法来区分不同的浏览器 1.为元素添加事件监听: 非IE:.addEventListener("click",show,false)//第三个参数 ...

  7. AndroidWear开发之HelloWorld篇

    通过前一篇的学习,我们把环境都搭建好了,这下我们就可以正真的开发了. 一.创建Wear项目 通过项目创建向导一步一步下去就可以创建好一个Wear项目: 1.新建项目,一次填入应用名字,应用包名,项目位 ...

  8. JS-提取字符串—>>普通方法VS正则表达式

    如下:“23nr qreq2 34fq4 4454gsr 45” [将这一串字符中的数字挑出来,当然人家作伴的不能被分开]. 一.普通方法: <script type="text/ja ...

  9. 旅游吧!我在这里 ——旅游相册POI搜索:找回你的足迹

    版权声明:本文由林少彬原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/89 来源:腾云阁 https://www.qclou ...

  10. jQuery数据缓存

    jQuery引入数据缓存机制的原因: 1.储存更DOM节点相关的数据.事件.动画等信息 2.用一种低耦合的方式让DOM节点和数据联系起来 实现原理: 1.jQuery内部创建cache对象 2.为需要 ...