我正在尝试在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...
并显示消息的原始结构。有什么办法藏起来吗?
答案 0 :(得分:1)
使用此命令:warnings.warn('No xlsx data files are fo...' , stacklevel=2)