【发布时间】:2018-03-26 20:54:20
【问题描述】:
我正在查看使用此代码的 INI 配置文件实现 here:
# Load the configuration file
with open("config.ini") as f:
sample_config = f.read()
config = ConfigParser.RawConfigParser(allow_no_value=True)
config.readfp(io.BytesIO(sample_config))
如果找不到配置文件,我想输出一些东西,但the Python documentation 没有说明else 条件,所以我想在这里使用if...else 块:
# Load the configuration file
if f = open("config.ini"):
sample_config = f.read()
config = ConfigParser.RawConfigParser(allow_no_value=True)
config.readfp(io.BytesIO(sample_config))
else
print "Could not open config file"
使用if...else 块代替with 块我会看到什么样的差异?
【问题讨论】:
标签: python python-2.7 if-statement with-statement