首页 » Python » python读取配置文件

python读取配置文件

 

python读取ini配置文件

#!/usr/bin/python
# -*- coding: utf-8 -*-
import ConfigParser,sys,os
CONFIGFILE  = os.getcwd() + '/tencent.ini'
CONFIGSECTION = 'Credentials'

def setup_credentials():
    config = ConfigParser.ConfigParser()
    try:
        config.read(CONFIGFILE)
        global access_key_id
        global access_key_secret
        access_key_id = config.get(CONFIGSECTION, 'accesskeyid')
        access_key_secret = config.get(CONFIGSECTION, 'accesskeysecret')
    except Exception, e:
		print traceback.format_exc()
		print("can't get access key pair, use config --id=[accesskeyid] --secret=[accesskeysecret] to setup")
		sys.exit(1)

ini文件格式如下:

[Credentials]
accesskeyid = 123
accesskeysecret = 123

 

 

原文链接:python读取配置文件,转载请注明来源!

0