Python之configparser配置文件的读取

Posted shen-qiang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python之configparser配置文件的读取相关的知识,希望对你有一定的参考价值。

配置文件名

config.ini

文件内容:

[linux]
ip:10.0.13.26
port:22
username:root
password:W2ynE6b58wheeFho

[mysql]
ip:10.0.13.26
port:22
username:root
password:W2ynE6b58wheeFho

 调用文件名

tryConfigparser.py

文件内容

#!/user/bin/env python
#coding:utf-8
#Author:shenqiang

import os
import configparser

‘‘‘拿到文件的路径‘‘‘
def base_path(filename = None):
    return os.path.join(os.path.dirname(__file__),filename)

def getConfigparser(Linux = linux):
    ‘‘‘实例化对象‘‘‘
    config = configparser.ConfigParser()
    ‘‘‘读取文件内容‘‘‘
    config.read(base_path(config.ini))
    
    ip = config.get(Linux,ip)
    port = config.get(Linux,port)
    username = config.get(Linux,username)
    password = config.get(Linux,password)
    return [ip,port,username,password]

for i in range(len(getConfigparser())):
    print(getConfigparser()[i])

 

以上是关于Python之configparser配置文件的读取的主要内容,如果未能解决你的问题,请参考以下文章

python之configParser模块读写配置文件

python模块之configparser

python ini文件处理

Python_55之configparser模块

Python之configparser配置文件的读取

Python 之ConfigParser