提问者:小点点

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个答案

匿名用户

使用此选项:warnings.warn('没有xlsx数据文件是FO.。。‘,StackLevel=2)