zoukankan      html  css  js  c++  java
  • 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])
  • 相关阅读:
    《将博客搬至CSDN》
    选课系统
    ATM_购物车
    python基础 面向对象编程
    python 基础 模块
    python基础 函数基础 模块:总复习
    第三篇:操作系统基础
    浅谈红黑树
    浅谈B和B+树
    第二篇:网络基础
  • 原文地址:https://www.cnblogs.com/shen-qiang/p/10765722.html
Copyright © 2011-2022 走看看