zoukankan      html  css  js  c++  java
  • python3 configparser 基本操作

    code

    import  configparser
     
    config = configparser.ConfigParser()
    file = 'config.ini'
    
    #添加section
    config.read(file)
    config.add_section('login')
    config.add_section('index')
    
    #添加option
    config.set('login','username','1111')
    config.set('login','password','2222')
    with open(file,'w+') as configfile:
        config.write(configfile)
    
    
    #是否存在section
    test1 = config.has_section("index")
    print(test1)
    #是否存在option
    test2 = config.has_option('login','username')
    print(test2)
    
    
    #返回所有可用的section
    section_list = config.sections()
    print(section_list)
    
    
    #返回指定section下的所有可用的option
    option_list = config.options("login")
    print(option_list)
    
    
    #读取option的值
    config.read(file)
    username = config.get('login','username')
    password = config.get('login','password')
    print(username,password)
    
    
    #移除option
    config.remove_option('login','username')
    with open(file,'w+') as configfile:
        config.write(configfile)
    
    
    #移除section
    config.remove_section('login')
    with open(file,'w+') as configfile:
        config.write(configfile)

  • 相关阅读:
    1.3、python内置类型(0529)
    1.2、Python快速入门(0529)
    1.1、Python快速入门(0529)
    mini Linux制作过程(25/01)
    samba基本应用24-4及示例
    Apache+Php+Mariadb+NFS+discuz
    U盘中病毒了怎么办
    bind9安装配置
    负载均衡的实现(1)
    MySQL之优化
  • 原文地址:https://www.cnblogs.com/sea-stream/p/14079159.html
Copyright © 2011-2022 走看看