当我尝试使用configparser.read读取一个不存在的文件不会产生异常,我认为它应该抛出异常。它没有。它将返回一个空列表。显然,我可以测试一个空列表并引发异常。它只是在我看来,更直观,更安全,如果configparser.read提出了一个FileNotFound例外。蟒蛇3 configparser.read()给出一个不存在的文件
[email protected]:~/nbmdt (blue-sky)*$ python3.6
Python 3.6.2 (default, Oct 2 2017, 16:51:32) [GCC 7.2.1 20170915 (Red Hat 7.2.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import configparser
>>> config=configparser.ConfigParser()
>>> config.read("xyzzy.xyz")
[]
>>> config.read("nbmdt.ini")
['nbmdt.ini']
>>>
谢谢
该文档解释说,这是这样,你可以看看在几个不同的地方一个配置文件:https://docs.python.org/2/library/configparser.html#ConfigParser.RawConfigParser.read。我想他们可能会抛出异常,如果没有发现。 – fdsa