python命令行参数';默认值';当使用配置文件时?
对于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
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
看起来怎么样?如何在配置文件的不同部分使用您的答案?