最近在写一个程序的过程中,遇到一个 Python 变量的变量的问题,就是某个变量的值,还是变量,然后我想取最终的值,感谢伟大的 google,写这篇文章记录下。先上代码:#!/usr/bin/env python
#encoding=utf-8import inspect
import ConfigParserconfPath="/home/linuxidc/config/config"def modify_config(domain_name, app_type, port, if_api, vip, svn, svnver, yfb_ip, cron_ip, hosts, hostname, if_cas_oa, if_session, if_nginx, check_apps_dir, health_check):
if vip is None:
vip=""
if yfb_ip is None:
yfb_ip=""
if cron_ip is None:
cron_ip=""
if hosts is None:
hosts=""
if if_cas_oa is None:
if_cas_oa=""
if if_session is None:
if_session=""
# 更新指定section, option的值
conf = ConfigParser.ConfigParser()
conf.read(confPath)
### 这个 inspect.getargspec 的作用是取函数的参数列表
arg_list = inspect.getargspec(modify_config).args
### 下面的 conf.set 的第二个参数我只想取 arg 的值,而 第三个参数想取 arg 变量值的值,因为 arg 的值本身就是 modify_config 函数的参数
for arg in arg_list:
conf.set("online", arg, locals().get(arg))
conf.write(open(confPath,"w"))if __name__ == "__main__":
domain_name = "www.linuxidc.com"
app_type = "osp"
port = "8081"
if_api = "1"
vip = "192.168.0.1"
svn = "https://svn.tools.linuxidc.com/svn/"
svnver = "24562"
yfb_ip = "192.168.0.1"
cron_ip = "192.168.0.2"
hosts = "www.linuxidc.com"
hostname = "JD-linuxidc-COM"
if_cas_oa = "0"
if_session = "mcs2"
if_nginx = "1"
check_apps_dir = "0"
health_check = "_health_check"
modify_config(domain_name, app_type, port, if_api, vip, svn, svnver, yfb_ip, cron_ip, hosts, hostname, if_cas_oa, if_session, if_nginx, check_apps_dir, health_check)下面关于Python的文章您也可能喜欢,不妨看看:Python:在指定目录下查找满足条件的文件 http://www.linuxidc.com/Linux/2015-08/121283.htmPython2.7.7源码分析 http://www.linuxidc.com/Linux/2015-08/121168.htm无需操作系统直接运行 Python 代码 http://www.linuxidc.com/Linux/2015-05/117357.htmCentOS上源码安装Python3.4 http://www.linuxidc.com/Linux/2015-01/111870.htm《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htmPython脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm在Ubuntu下用Python搭建桌面算法交易研究环境 http://www.linuxidc.com/Linux/2013-11/92534.htmPython 语言的发展简史 http://www.linuxidc.com/Linux/2014-09/107206.htmPython 的详细介绍:请点这里
Python 的下载地址:请点这里本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-09/122632.htm