ConfigParser 实例

#coding=utf-8

import ConfigParser

def writeConfig(filename):

config = ConfigParser.ConfigParser()

# set db

section_name = ‘db‘

config.add_section( section_name  )

config.set( section_name, ‘dbname‘, ‘MySQL‘)

config.set( section_name, ‘host‘, ‘127.0.0.1‘)

config.set( section_name, ‘port‘, ‘80‘)

config.set( section_name, ‘password‘, ‘123456‘)

config.set( section_name, ‘databasename‘, ‘test‘)

# set app

section_name = ‘app‘

config.add_section( section_name  )

config.set( section_name, ‘loggerapp‘, ‘192.168.20.2‘)

config.set( section_name, ‘reportapp‘, ‘192.168.20.3‘)

# write to file

config.write( open(filename, ‘a‘) )

def updateConfig(filename, section, **keyv):

config = ConfigParser.ConfigParser()

config.read(filename)

[config.set(section, key, keyv[key]) for key in keyv if config.has_option(section, key)]

config.write( open(filename, ‘r+‘) )

if __name__ == ‘__main__‘:

file_name = r‘C:\Users\Administrator\Desktop\test.ini‘

writeConfig(file_name)

updateConfig(file_name, ‘app‘, reportapp = ‘192.168.100.100‘)

print "end__"

ConfigParser 实例

时间: 2024-07-30 03:47:39

ConfigParser 实例的相关文章

ConfigParser 实例 02

# -*- coding: cp936 -*- IP1=""  #扫描IP IP2=""   #当前已经扫到的IP INITXT="IP.ini"  #INI文件名字 import ConfigParser def ini_get():  #读取INI try: global IP1 global IP2 global INITXT config = ConfigParser.ConfigParser() config.readfp(open(I

ConfigParser 实例 01

# -*- coding: utf-8 -*-import ConfigParserconfig = ConfigParser.ConfigParser()config.readfp(open('ixamail.ini'))a = config.get("host","smtp_server")print aconfig.add_section("book")config.set("book", "title&quo

python 关于操作文件的相关模块(os,sys,shutil,subprocess,configparser)

一:os模块 os模块提供了许多允许你程序与操作系统直接交互的功能 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cd os.curdir 返回当前目录: ('.') os.pardir 获取当前目录的父目录字符串名:('..') os.makedirs('dirname1/dirname2') 可生成多层递归目录 os.removedirs('dirname1') 若目录

Python中配置文件解析模块-ConfigParser

Python中有ConfigParser类,可以很方便的从配置文件中读取数据(如DB的配置,路径的配置).配置文件的格式是: []包含的叫section, section 下有option=value这样的键值该模块的常用方法 1.config=ConfigParser.ConfigParser() 创建ConfigParser实例 2.config.sections() 返回配置文件中节序列 3.config.options(section) 返回某个项目中的所有键的序列 4.config.g

pyton简单网络爬虫,aspx网站中form使用到了__VIEWSTATE、__EVENTVALIDATION、cookie来验证的提交

一.需求 最近在学习Python,刚好有一个需求,需要从一个本地密码管理系统获取本地账号的密码 .之前的流程是 登录web-----输入计算机名-----管理员账号.密码---提交------页面返回密码,复制密码,发送邮件给请求用户.每次都要登录页面,这个很郁闷,于是记录下整个过程也有助于自己学习 二.页面分析 先来看下整个流程:输入选项,查询就会返回 让我们先看看页面是什么,竟然有两个隐藏参数,每次提交的时候竟然每次都会变,这是一个坑,后面会提到 不管了,我用的是Chrome ,直接F12,

python 对模块的应用你还得练点这些

1.有如下字符串:n = "路飞学城"(编程题) - 将字符串转换成utf-8的字符编码的字节,再将转换的字节重新转换为utf-8的字符编码的字符串 - 将字符串转换成gbk的字符编码的字节,再将转换的字节重新转换为utf-8的字符编码的字符串 n = '路飞学诚' print(n.encode('utf-8')) # b'\xe8\xb7\xaf\xe9\xa3\x9e\xe5\xad\xa6\xe8\xaf\x9a' print(n.encode('utf-8').decode('

python ConfigParse模块中的方法

1.config=ConfigParser.ConfigParser() 创建ConfigParser实例 2.config.sections() 返回配置文件中节序列 3.config.options(section) 返回某个项目中的所有键的序列 4.config.get(section,option) 返回section节中,option的键值 5.config.add_section(str) 添加一个配置文件节点(str) 6.config.set(section,option,val

python 常用模块之ConfigParser

在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在Python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser, Python ConfigParser模块解析的配置文件的格式比较象ini的配置文件格式 下面用实例说明如下: 配置文件db.conf [db] db_host=10.1.10.15 db_port=3306 db_user=root db_pass=59222999 连接数据程序如下: #!/usr/bin/en

解析配置文件ConfigParser模块

一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类似于key-value 的配置内容. 1 [mongoDb] #-------->section 2 userName=dsg 3 passWord=dsg 4 dataBase=promo 5 tableName=trace 6 mongodb_ip_port=127.0.0.1:3717 7 8 [filePath]#---