Savina Dimitrova
Savina Dimitrova

Reputation: 155

Python warnings - any way to remove the raw warning output at the end of a custom warning message

I'm trying to create a custom warning message in a Python script:

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. ')

The problem is that during execution of the script, if I trigger this message the output looks like:

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. ')

I don't want this warnings.warn... on the last row to appear and to show the raw structure of the message. Any way to hide it?

Upvotes: 2

Views: 523

Answers (1)

H. pap
H. pap

Reputation: 351

use this: warnings.warn('No xlsx data files are fo...' , stacklevel=2)

Upvotes: 3

Related Questions