当前位置: 移动技术网 > IT编程>脚本编程>Python > Python如何输出警告信息

Python如何输出警告信息

2020年07月31日  | 移动技术网IT编程  | 我要评论

问题

你希望自己的程序能生成警告信息(比如废弃特性或使用问题)。

解决方案

要输出一个警告消息,可使用 warning.warn() 函数。例如:

import warnings

def func(x, y, logfile=none, debug=false):
  if logfile is not none:
     warnings.warn('logfile argument deprecated', deprecationwarning)
  ...

warn() 的参数是一个警告消息和一个警告类,警告类有如下几种:userwarning, deprecationwarning, syntaxwarning, runtimewarning, resourcewarning, 或 futurewarning.

对警告的处理取决于你如何运行解释器以及一些其他配置。 例如,如果你使用 -w all 选项去运行python,你会得到如下的输出:

bash % python3 -w all example.py
example.py:5: deprecationwarning: logfile argument is deprecated
  warnings.warn('logfile argument is deprecated', deprecationwarning)

通常来讲,警告会输出到标准错误上。如果你想讲警告转换为异常,可以使用 -w error 选项:

bash % python3 -w error example.py
traceback (most recent call last):
  file "example.py", line 10, in <module>
    func(2, 3, logfile='log.txt')
  file "example.py", line 5, in func
    warnings.warn('logfile argument is deprecated', deprecationwarning)
deprecationwarning: logfile argument is deprecated
bash %

讨论

在你维护软件,提示用户某些信息,但是又不需要将其上升为异常级别,那么输出警告信息就会很有用了。 例如,假设你准备修改某个函数库或框架的功能,你可以先为你要更改的部分输出警告信息,同时向后兼容一段时间。 你还可以警告用户一些对代码有问题的使用方式。

作为另外一个内置函数库的警告使用例子,下面演示了一个没有关闭文件就销毁它时产生的警告消息:

>>> import warnings
>>> warnings.simplefilter('always')
>>> f = open('/etc/passwd')
>>> del f
__main__:1: resourcewarning: unclosed file <_io.textiowrapper name='/etc/passwd'
 mode='r' encoding='utf-8'>
>>>

默认情况下,并不是所有警告消息都会出现。-w 选项能控制警告消息的输出。 -w all 会输出所有警告消息,-w ignore 忽略掉所有警告,-w error 将警告转换成异常。 另外一种选择,你还可以使用 warnings.simplefilter() 函数控制输出。 always 参数会让所有警告消息出现,`ignore 忽略调所有的警告,error 将警告转换成异常。

对于简单的生成警告消息的情况这些已经足够了。 warnings 模块对过滤和警告消息处理提供了大量的更高级的配置选项。 更多信息请参考 python文档

以上就是python如何输出警告信息的详细内容,更多关于python 输出警告信息的资料请关注移动技术网其它相关文章!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网