相关疑难解决方法(0)

python 3 configparser.read() 在给定不存在的文件时不会引发异常

当我尝试使用 configparser.read 读取不存在的文件时,我认为它应该引发异常。它没有。它返回一个空列表。显然,我可以测试一个空列表并引发异常。如果 configparser.read 引发 FileNotFound 异常,在我看来它更直观和更安全。

jeffs@jeffs-laptop:~/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']
 >>>
Run Code Online (Sandbox Code Playgroud)

谢谢

python configparser

4
推荐指数
1
解决办法
1435
查看次数

标签 统计

configparser ×1

python ×1