Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python命令行参数';默认值';当使用配置文件时?_Python_Python 3.x_Command Line Arguments_Configuration Files - Fatal编程技术网

python命令行参数';默认值';当使用配置文件时?

python命令行参数';默认值';当使用配置文件时?,python,python-3.x,command-line-arguments,configuration-files,Python,Python 3.x,Command Line Arguments,Configuration Files,对于python脚本,我有以下命令行参数: command_parser.add_argument("--wateva", help="Don't care.", default=42) 现在我需要从命令行参数切换到配置文件,因为脚本已经增长了很多,要传递所有参数而不输入错误是不可能的 如何为从Python中的配置文件读取的参数附加默认值(即config=configparser.configparser())可以通过将默认值作为字典传递到configparser构造函数来指定默认值 impo

对于python脚本,我有以下命令行参数:

command_parser.add_argument("--wateva", help="Don't care.", default=42)
现在我需要从命令行参数切换到配置文件,因为脚本已经增长了很多,要传递所有参数而不输入错误是不可能的


如何为从Python中的配置文件读取的参数附加默认值(即
config=configparser.configparser()

可以通过将默认值作为字典传递到configparser构造函数来指定默认值

import ConfigParser

# New instance with 'bar' and 'baz' defaulting to 'Life' and 'hard' each
config = ConfigParser.SafeConfigParser({'bar': 'Life', 'baz': 'hard'})
config.read('example.cfg')

print config.get('Section1', 'foo') # -> "Python is fun!"
config.remove_option('Section1', 'bar')
config.remove_option('Section1', 'baz')
print config.get('Section1', 'foo') # -> "Life is hard!"

example.cfg
看起来怎么样?如何在配置文件的不同部分使用您的答案?