Python警告-在自定义警告消息末尾删除原始警告输出的任何方法

时间:2020-07-07 14:29:33

标签: python warnings

我正在尝试在Python脚本中创建自定义警告消息:

allfiles = glob.glob(folderpath + "\\*.xlsx")
if not allfiles:
    warnings.warn('No xlsx data files are found in the folder "'+folderpath+ '"!\n Please note that currently the program could process only this data format. ')

问题在于,在脚本执行期间,如果我触发此消息,则输出如下:

UserWarning: No xlsx data files are found in the folder "C:\Users\Savina\Desktop\project\"!
 Please note that currently the program could process only this data format. 
  warnings.warn('No xlsx data files are found in the folder "'+filepath+ '"!\n Please note that currently the program could process only this data format. ')

我不想在最后一行显示此warnings.warn...并显示消息的原始结构。有什么办法藏起来吗?

1 个答案:

答案 0 :(得分:1)

使用此命令:warnings.warn('No xlsx data files are fo...' , stacklevel=2)