python调用配置文件实例

python脚本中涉及配置文件的调用实例 配置文件: config.ini
[test] name = 'eric' ip = ['192.168.0.12','192.168.0.13']
python调用主程序
#!/usr/bin/env python

import os,sys
import ConfigParser

class Get_data:
    def __init__(self,project,list,config_file_path):
        'get data from config.ini'
        dataConfig = ConfigParser.ConfigParser()
        dataConfig.read(config_file_path)

        self.data = dataConfig.get(project,list)
        self.data = self.data[1:-1]
        self.data = self.data.split(',')

    def display(self):
        if len(self.data) == 1:
            for i in self.data:
                print i
        else:
            for i in self.data:
                print i[1:-1]

if __name__ == "__main__":
    getdata = Get_data("test","name",'./config.ini')
    getdata.display()

    getdata = Get_data("test","ip",'./config.ini')
    getdata.display()

你可能感兴趣的:(python,脚本)