• python读写ini配置文件


    像邮箱等信息是可以写在配置文件里面的,python有一个配置模块ConfigParser,可以处理配置文件信息

    目录

    1、配置模块ConfigParser

    2、基本应用

    1、配置模块ConfigParser

    关于ini文件:

    ini文件中,每一部分以[section]开始 option=value结尾;备注以;开头;section不可重名

    如:

    用ConfigParser模块中的ConfigParser类读取ini文件,然后使用ConfigParser类中的get方法,然后读取到value值

    conf=ConfigParser.ConfigParser()
    key1=conf.get("section","option") # 读取到第一个section的中的option值
    key2=conf.get("section2","option")# 读取到第二个section的中的option值
    
    

      

    2、基本应用

    第一步:在pycharm中创建一个.ini文件

    new->file ,命名为new.ini,写入信息如下:

    [email_qq]
    
    sender=123@qq.com
    psw=123456
    port=465
    smtp_server=smtp.qq.com
    receiver=456@qq.com

    第二步:在pycharm中创建config.py文件,调用ConfigParser模块中方法读取

    # coding:utf-8
    
    import ConfigParser
    import os
    #用os模块来读取
    curpath=os.path.dirname(os.path.realpath(__file__))
    cfgpath=os.path.join(curpath,"peizhi.ini")  #读取到本机的配置文件
    
    
    #调用读取配置模块中的类
    conf=ConfigParser.ConfigParser()
    
    #调用get方法,然后获取配置的数据
    sender=conf.get("email_qq","sender")
    psw=conf.get("email_qq","psw")
    stmp=conf.get("email_qq","stmp")
    port=conf.get("email_qq","port")

    第三步:在pycharm中run_allcase.py文件的main函数中,调用配置读取配置文件

    #调用配置文件的模块
    from config import config
    
    #调用这个模块中的参数
    sender=config.sender
    psw=config.psw
    receiver=config.receiver
    port=config.port
    
    #再调用run_allcase中发送邮件的方法,将值填进去
    send_mail(sender,psw,receiver,port)
    

      

  • 相关阅读:
    c#中的as,is和强转
    Shader中的lerp
    [RequireComponent(typeof(....))]
    [ExecuteInEditMode]
    在ugui上显示3d物体
    T4语法快速入门
    MVC生命周期
    MVC5路由系统机制详细讲解
    FluentScheduler定时器计划任务
    MVC的WebViewPage
  • 原文地址:https://www.cnblogs.com/weizhideweilai/p/9515046.html
Copyright © 2020-2023  润新知